11

How to get java version and want to get '6' out of java version from batch file. I tried below script, but it didn't 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 "C:\Program Files\Java\jdk1.6.0_31" value.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
Bobby Rachel
  • 453
  • 6
  • 10
  • 20

5 Answers5

21
for /f tokens^=2-5^ delims^=.-_^" %j in ('java -fullversion 2^>^&1') do @set "jver=%j%k%l%m"

This will store the java version into jver variable and as integer And you can use it for comparisons .E.G

if %jver% LSS 16000 echo not supported version

.You can use more major version by removing %k and %l and %m.This command prompt 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 (as it uses only internal commands and not external ones as FIND,FINDSTR and does not use GOTO which also can slow the script). Some JDK vendors does not support -fullversion switch or their implementation is not the same as this one provided by Oracle (better avoid them).

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • How to print jver value. i tried with echo %jver%. its not working – Bobby Rachel Jul 18 '13 at 05:00
  • try with ***PATH %PATH%;%JAVA_HOME%\bin\\*** before the for loop – npocmaka Jul 18 '13 at 05:09
  • getting error: it says *path is not a command. then for echo %jver% it says echo is off. i updated my question above. gave full script. just check it out plz. – Bobby Rachel Jul 18 '13 at 05:15
  • remove the *.The bolding left one asteriks here...Will edit the answer – npocmaka Jul 18 '13 at 05:21
  • java.exe is not in the path. what is the result if you just write %JAVA_HOME%\bin\java.exe /? – npocmaka Jul 18 '13 at 06:03
  • your path should have %JAVA_HOME%\bin.You can check if `java -fullversion` generates some output.If you have the java.exe in the path i should rewrite my answer to use only `-version` switch. – npocmaka Jul 18 '13 at 06:53
  • here is my code: for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -version 2^>^&1') do set "jver=%%j" i got 6 out of the version. how to get "0_31"? – Bobby Rachel Jul 18 '13 at 07:00
  • you must use `-fullversion` instead `-version` – npocmaka Jul 18 '13 at 07:03
  • `for /f eol^=J^ tokens^=2-5^ delims^=.-_^" %%j in ('java -version 2^>^&1') do set "jver=%%j.%%k.%%l_%%m"` – npocmaka Jul 18 '13 at 07:06
  • I'd like to use a custom java path, like this: `for /f tokens^=2-5^ delims^=.-_^" %j in ('path\to\jre\java -fullversion 2^>^&1') do @set "jver=%j%k%l%m"`. How do I get this working? – eye_mew Apr 24 '14 at 00:07
  • @s1ice - it should work in the way you posted it.But some vendors have different implementations of `-fullversion` . What is the problem with the line you posted? Is it ran from batch file or from command prompt? – npocmaka Apr 24 '14 at 03:58
3

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

output is: java version: "1.7.0_21-b11"
awk for Windows

Endoro
  • 37,015
  • 8
  • 50
  • 63
1

it will be surly idiot but why do not just print JAVA_HOME path ?

cyril
  • 872
  • 6
  • 29
0

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

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

Richie
  • 9,006
  • 5
  • 25
  • 38
0

Within a block statement, %var% would be echoed as the value of var BEFORE the block was entered.

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

Magoo
  • 77,302
  • 8
  • 62
  • 84