开发者

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

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 nee开发者_运维技巧d 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 :)


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 )


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


You can try:

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


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


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\""


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%
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜