简体   繁体   中英

Set Java Major Version Number As A Variable

This is irritating. I am trying to incorporate a trigger in a script to run if the major version of Java is 1.6,1.7, etc.

In the line I am trying to set the variable in, I pull the Java version and pipe it to for with tokens and delims identified, resulting in the "do" as a set command for variable %jver%. However, echoing %jver% results in "echo is on". Why wont it set the variable? Everything looks legit until %jver% is used.

Yes, I double the percentages for the script. The code here is for use at the command prompt.

Here is the line:

%systemroot%\system32\java.exe -version 2>&1 | for /f "tokens=1-4 delims=. " %a in ('findstr /i "version"') do (set jver=%~c.%d)

@rao thank you for helping me discover an alternate solution to meet my intent.

My workaround solution to this is the following line:

for /f "tokens=1-4 delims=. " %a in ('java -version 2^>^&1 ^| findstr /i "version"') do (SET JVER=%~c.%d)

Looks this is answered in this post by Patrick Cuff

Here is the solution adding from mentioned original post

@echo off
setlocal

set VERSION6="1.6.0_21"
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
    @echo Output: %%g
    set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
@echo Output: %JAVAVER%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM