Get java version from batch file

How to get java version and want to get "6" from java version from batch file. I tried below script, but this did not work.

REM check java exists using JAVA_HOME system variable if "%JAVA_HOME%" == "" ( ECHO Installing java start /w jdk.exe /s SETX -m JAVA_HOME "C:\Program Files\Java\jdk1.6.0_31" ECHO java installed successfully ) ELSE ( ECHO checking java version goto check_java_version ) REM check java version using JAVA_HOME system variable :check_java_version set PATH=%PATH%;%JAVA_HOME%\bin for /f tokens^=2-5^ delims^=.-_^" %%j in ('%JAVA_HOME%/bin/java -version 2^>^&1') do set "jver=%%j%%k%%l%%m" echo %jver% 

JAVA_HOME has the value "C: \ Program Files \ Java \ jdk1.6.0_31".

+7
command cmd batch-file version
source share
5 answers
 for /f tokens^=2-5^ delims^=.-_^" %j in ('java -fullversion 2^>^&1') do @set "jver=%j%k%l%m" 

This will save the java version in the jver variable and as an integer And you can use it to compare .EG

 if %jver% LSS 16000 echo not supported version 

. You can use the larger version by deleting% k and% l and% m. This is the command line version.

For .bat use this:

 @echo off PATH %PATH%;%JAVA_HOME%\bin\ for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do set "jver=%%j%%k%%l%%m" 

According to my tests, this is the fastest way to get the Java version from bat (since it uses only internal commands and not external like FIND , FINDSTR and does not use GOTO , which can also slow down the script). Some JDK providers do not support the -fullversion switch or their implementation is not the same as in Oracle Oracle (it is better to avoid them).

+15
source share

You can do this with awk :

  > java -fullversion 2> & 1 | awk "{print $ NF}"
 "1.7.0_21-b11"

 > java -fullversion 2> & 1 | awk -F \ "" {print $ (NF-1)} "
 1.7.0_21-b11

Script example:

 @ECHO OFF &SETLOCAL FOR /f %%a IN ('java -fullversion 2^>^&1^|awk "{print $NF}"') DO SET "javaversion=%%a" IF DEFINED javaversion (ECHO java version: %javaversion%) ELSE ECHO java NOT found 

: java version: "1.7.0_21-b11"
awk for windows

+3
source share

it should be java -version // if the environment path is already set

or% JAVA_HOME% / bin / java -version // if the path is not already set

0
source share

In a block expression,% var% will be displayed as the value of var BEFORE the block.

Move echo outside the block or echo %%g or call echo %%javaver%% or call SETLOCAL enabledelayedexpansion and echo !javaver!

0
source share

It will be a terrible idiot, but why not just print the JAVA_HOME path?

0
source share

All Articles