I want to extract the Java version on my machine.
1/ I have an Oracle Java 8 installed and the command where java
outputs this:
C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
2/ The command java -version
outputs this:
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
3/ The command java -version 2>&1 | find "version"
outputs this:
java version "1.8.0_221"
So I want to do the exact same thing as the last command within a batch file. So within a batch file, I have this code similar to step 2:
@echo off & setLocal enableExtensions enableDelayedExpansion
set /a count=0
for /f "tokens=*" %%j in ('where java') do (
set /a count+=1
set JAVA[!count!]=%%j
call :echo_java
)
goto :eof
:echo_java
set java_cmd="!JAVA[%count%]!" -version
for /f "tokens=*" %%v in ('%java_cmd% 2^>^&1') do echo %%v
exit /b 0
But as soon as I modify the function :echo_java
to be the equivalent to step 3 like this:
:echo_java
set java_cmd="!JAVA[%count%]!" -version
for /f "tokens=*" %%v in ('%java_cmd% 2^>^&1 ^| find "version"') do echo %%v
exit /b 0
I get this error:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
I suppose that there are some bad escaping happening here but I really can't see what's wrong.