0

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)
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
ZNDR
  • 1

2 Answers2

0

@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)

ZNDR
  • 1
-1

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%
Community
  • 1
  • 1
Rao
  • 20,781
  • 11
  • 57
  • 77
  • I should have specified my intensions with this code; I want a single line or command to be able to use just the two portions of the version for a variable or immediately perform certain actions if a certain version is installed. This was able to help me look at it from another point of view though. Thank you. You were vital to me finding an alternative solution. Although, it still bothers me that my original command did not work despite looking like it worked until the variable is used. – ZNDR Jan 17 '15 at 00:41
  • You may still use this in single line `for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (set jver=%%g)` – Rao Jan 17 '15 at 00:44
  • That does work. I wanted to be able to individually use each part of the version independent of one another. This way I can leave out the build or use it as needed. – ZNDR Jan 17 '15 at 01:02
  • for /f "tokens=1-6 delims=._ " %a in ('java -version 2^>^&1 ^| findstr /i "version"') do (SET JVER=%~c.%d.%f) – ZNDR Jan 17 '15 at 01:04