批处理文件 java,从批处理文件获取Java版本

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值