2

My question, is extremely similar to the following SO question: How to get Java Version from batch script?

In fact, it did almost solve my problem. The only difference is that I've to check the Java version based on %JAVA_HOME%, which the user is free to modify. The issue that I'm facing is with this code:

@ECHO OFF
SETLOCAL enableextensions enabledelayedexpansion

IF "%JAVA_HOME%"=="" (
    @ECHO Please set JAVA_HOME environment variable
    EXIT /B
) 

@echo "%JAVA_HOME%"

REM Checking JAVA_VERSION
SET JAVA_VERSION=
FOR /f "tokens=3" %%g in ('"%JAVA_HOME%"\bin\java -version 2^>^&1 ^| findstr /i "version"') do (
        SET "JAVA_VERSION=%%g"
)

%JAVA_HOME%% in my system points to "C:\Program Files\jdk1.7.0_25" (notice the space in the path)

Even with the quotes, I get the following error in command line:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

Any idea as to how to solve this problem? (The comments to the aforementioned article also mentions this issue). I'm working on a Windows 7 machine

Community
  • 1
  • 1
Sujay
  • 6,753
  • 2
  • 30
  • 49

3 Answers3

3
FOR /f "tokens=3" %%g in ('"%JAVA_HOME%\bin\java" -version 2^>^&1 ^| findstr /i "version"') do (
        SET "JAVA_VERSION=%%g"
)
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Still get the same error. I had actually tried this as well. If I echo %JAVA_VERSION% after this, it shows that the variable has stored "-version" as its value – Sujay Aug 09 '13 at 18:15
  • Maybe, but you doesn't get the error: `'C:\Program' is not recognized as an internal or external command, operable program or batch file.`. – Endoro Aug 09 '13 at 18:29
  • I do the same error Endoro. It's just I added one more echo statement after the for to see if JAVA_VERSION comes empty or not. That's where it shows "-version" as the value for this variable. This is not related to my actual problem – Sujay Aug 09 '13 at 18:38
  • try `"%JAVA_HOME%\bin\java" -version` on the command line window. – Endoro Aug 09 '13 at 18:50
  • That did strike my mind and when I paste it directly on command line, it works. I can't figure out why it fails in for statement. – Sujay Aug 09 '13 at 20:39
  • you should check for a c&p error, the name of the bach and for the Batch file encoding type. – Endoro Aug 09 '13 at 20:48
  • name of the batch/c&p isn't the issue. been trying to figure this out for a while now. how to change batch file encoding? – Sujay Aug 09 '13 at 23:52
1

Edit the %JAVA_HOME% Variable into:

C:\"Program Files"\jdk1.7.0_25

if you want it automated, type

set %JAVA_HOME%=C:\"Program Files"\jdk1.7.0_25

REASON WHY: The batch file does not accept the quotes; It is identifying them as a single file. So it attempts to find "C:\Program Files\jdk1.7.0_25" NOT as a folder path, but as a folder NAME in your root folder. If you type in C:\"Program Files"\jdk1.7.0.25 it identifies that "Program Files" is a single file. If there are no redirection operators, It would think that the path would be like this; C:\Program\Files\jdk1.7.0_25. It worked for me; It should probably work for you.

Hope that helped

-SonorousTwo

Epic_Tonic
  • 96
  • 1
  • 9
0

I had a similar problem, take a look at my QA: How to get Java version in a batch script subroutine?

From my answer:

It seems that piping the output to findstr was stripping the quotes for some reason.

I managed to fix the problem without Epic_Tonic's workaround (which would be very difficult to do with a path as a parameter).


This should work in your case:

set first=1
for /f "tokens=3" %%g in ('"%JAVA_HOME%\bin\java" -version 2^>^&1') do (
    if !first!==1 echo %%~g
    set first=0
)

Note: first is for only searching the first line of java -version's output (reference).

Sean McCallum
  • 158
  • 2
  • 10