How can I find the java version and set that to a variable?
I've tried this:
for /f "tokens=*" %%a in ('java -version | find "version"') do (set var1=%%a)
but java isn't redirecting its output to find
. Another post suggested this solution
java -version 2>&1 | findstr "version" >tmp.txt
for /f "tokens=*" %%x in (tmp.txt) do (set var1=%%x)
echo %var1%
del tmp.txt
but I would like to avoid using the temp file.
java.exe
seems to output the version information at STDERR, so the correct code is:As you can see,
>
,&
and|
are escaped with^
.