How to get java version using single command in Windows command prompt?

痞子三分冷 提交于 2019-12-23 12:26:21

问题


I need to fetch java version 1.6.0_26 from the below java -version output

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)

Please help me getting 1.6.0_26

Note: I don't need power shell or any external programs

UPDATE

I came up with java -version 2>&1 | findstr /i "version" which give me below output

java version "1.6.0_22"

now even a java way of pattern matching or regex will work for me :)


回答1:


You can run the solution from the question Pangea linked to in a single line:

    
c:\>for /f "tokens=3" %g in ('java -version 2^>^&1 ^| findstr /i "version"') do @echo %g
"1.6.0_24"
c:\>for /f "tokens=3" %g in ('java -version 2^>^&1 ^| findstr /i "version"') do ( @set v=%g & @echo %v:~1,8% )
1.6.0_24

I just checked and it seems my second example only works with Windows7, the following works for me on Windows XP (so it should work on Windows7 as well)

for /f "tokens=3" %g in ('java -version 2^>^&1 ^| findstr /i "version"') do ( @echo %~g )



回答2:


you can do it from single line from command promt C:>java -version




回答3:


You can try:

java -version 2>&1 | awk '/version/ {print $3}' | egrep -o '[^\"]*'



回答4:


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




回答5:


I wrote the below function to fetch java version 1.6.0_26

String regexMatch(String myInput, String myRegex)
{
String ResultString
Pattern regex
Matcher regexMatcher

regex = Pattern.compile(myRegex, Pattern.DOTALL);

regexMatcher = regex.matcher(myInput);

List<String> matchList = new ArrayList<String>();
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group());
}

for(int i=0;i<matchList.size();i++)
{
    myInput=myInput.replaceAll( matchList[i], '')
}
return myInput;
}

and call

rs=regexMatch(rs,"[a-zA-Z\"]+")

where rs="java version \"1.6.0_26\""




回答6:


Based on @a_horse_with_no_name but detecting first if command exists.

where java > nul
if %ERRORLEVEL% neq 0 (
    set java_v="Command not found"
) else (
    for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do set java_v=%%g
)
echo  Java: %java_v:~1,8%


来源:https://stackoverflow.com/questions/7606607/how-to-get-java-version-using-single-command-in-windows-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!