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

3 Answers 3

3
FOR /f "tokens=3" %%g in ('"%JAVA_HOME%\bin\java" -version 2^>^&1 ^| findstr /i "version"') do (
        SET "JAVA_VERSION=%%g"
)
Sign up to request clarification or add additional context in comments.

7 Comments

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
Maybe, but you doesn't get the error: 'C:\Program' is not recognized as an internal or external command, operable program or batch file..
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
try "%JAVA_HOME%\bin\java" -version on the command line window.
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.
|
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

1 Comment

That makes sense. Thanks for the explanation and the workaround!
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.