如何从批处理脚本中获取 Java 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5675459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:14:01  来源:igfitidea点击:

How to get Java Version from batch script?

javabatch-file

提问by AabinGunz

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

我试图从下面给出的 java 版本输出中获取 '6'

java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)

For the same I wrote this batch script

同样我写了这个批处理脚本

set VERSION6="1.6.0_21"
java -version 2>&1 | findstr "version" >ab.txt
for /f "tokens=3" %%g in (ab.txt) do (
  if not %%g == %VERSION6% echo %%g
  echo %%g
)

%%gdisplays "1.6.0_21"

%%g显示“1.6.0_21”

May someone guide me to correct direction? I am not much familiar with for /f.

有人可以指导我正确的方向吗?我不太熟悉for /f

回答by Patrick Cuff

@echo off
setlocal

set VERSION6="1.6.0_21"
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
    @echo Output: %%g
    set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
@echo Output: %JAVAVER%

for /f "delims=. tokens=1-3" %%v in ("%JAVAVER%") do (
    @echo Major: %%v
    @echo Minor: %%w
    @echo Build: %%x
)

endlocal

In the first forloop, "tokens=3"says that we're going to just use the third token from the command output. Rather than redirect the output of the java -versioncommand to a file, we can run this command within the forloop itself. The carets (^) are escape characters, and are needed so we can embed the >, &and |symbols in the command string.

在第一个for循环中,"tokens=3"表示我们将只使用命令输出中的第三个标记。java -version我们可以在for循环本身内运行此命令,而不是将命令的输出重定向到文件。^插入符号( ) 是转义字符,需要它以便我们可以在命令字符串中嵌入>,&|符号。

Within the body of the forloop, we set a new var, JAVAVER, so that we can do some manipulation of the version string later.

for循环体中,我们设置了一个新的 var, JAVAVER,以便我们稍后可以对版本字符串进行一些操作。

The set JAVAVER=%JAVAVER:"=%command removes the double quotes from around the version string.

set JAVAVER=%JAVAVER:"=%命令从版本字符串周围删除双引号。

The last forloop parses the java version string. delims=.says we're going to delimit tokens using periods. tokens=1-3says we're going to pass the first three tokens from the string to the body of the loop. We can now get the components of the java version string using the explicit variable, %%vand the implied variables (next letters in the alphabet) %%wand %%x.

最后一个for循环解析 java 版本字符串。delims=.表示我们将使用句点分隔标记。tokens=1-3表示我们要将字符串中的前三个标记传递给循环体。我们现在可以使用显式变量%%v和隐式变量(字母表中的下一个字母)%%w%%x.

When I run this on my system I get:

当我在我的系统上运行它时,我得到:

Output: "1.6.0_24" 
Output: 1.6.0_24
Major: 1 
Minor: 6 
Build: 0_24

回答by Owen Fraser-Green

This will extract the minor part of the version number:

这将提取版本号的次要部分:

java -version 2>&1 | awk '/version/ {print }' | awk -F . '{print }'

However, it may be better to extract the major.minor and match on that in case Oracle ever change the version number scheme again e.g.:

但是,最好提取major.minor并与之匹配,以防Oracle再次更改版本号方案,例如:

java -version 2>&1 | awk '/version/ {print }' | egrep -o '[0-9]+\.[0-9]+'

回答by Eugene Yokota

I've made some modification to Patrick's answerso it works with Java 9, 10, etc. This returns minor version for 1.x, and major version for Java 9, 10, etc.

我对帕特里克的回答做了一些修改,所以它适用于 Java 9、10 等。这将返回 1.x 的次要版本,以及 Java 9、10 等的主要版本。

@echo off
setlocal

rem We use the value the JAVACMD environment variable, if defined
rem and then try JAVA_HOME
set "_JAVACMD=%JAVACMD%"
if "%_JAVACMD"=="" (
  if not "%JAVA_HOME%"=="" (
    if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
  )
)
if "%_JAVACMD%"=="" set _JAVACMD=java

rem Parses x out of 1.x; for example 8 out of java version 1.8.0_xx
rem Otherwise, parses the major version; 9 out of java version 9-ea
set JAVA_VERSION=0
for /f "tokens=3" %%g in ('%_JAVACMD% -Xms32M -Xmx32M -version 2^>^&1 ^| findstr /i "version"') do (
  set JAVA_VERSION=%%g
)
set JAVA_VERSION=%JAVA_VERSION:"=%
for /f "delims=.-_ tokens=1-2" %%v in ("%JAVA_VERSION%") do (
  if /I "%%v" EQU "1" (
    set JAVA_VERSION=%%w
  ) else (
    set JAVA_VERSION=%%v
  )
)

@echo %JAVA_VERSION%

endlocal

回答by npocmaka

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 jvervariable and as integer And you can use it for comparisons .E.G

这会将 java 版本存储到jver变量和整数中,您可以将其用于比较 .EG

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.

.您可以通过删除 %k 和 %l 和 %m 来使用更多主要版本。此命令提示版本。

For .bat use this:

对于 .bat 使用这个:

@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,FINDSTRand does not use GOTOwhich also can slow the script). Some JDK vendors does not support -fullversionswitch or their implementation is not the same as this one provided by Oracle (better avoid them).

根据我的测试,这是从 bat 获取 java 版本的最快方法(因为它仅使用内部命令而不使用外部命令 as FINDFINDSTR并且不使用GOTO也会减慢脚本的速度)。一些JDK厂商不支持-fullversionswitch或者它们的实现与Oracle提供的不同(最好避免它们)。