How to get Java Version from batch script?

前端 未结 4 1384

I am trying to get \'6\' out of the java version output given below

java version \"1.6.0_21\"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSp         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 18:28

    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).

提交回复
热议问题