4

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

3
  • Describe your end goal? Perhaps there is a better way. And isn't this the same question as stackoverflow.com/questions/5675459/… Commented Sep 30, 2011 at 6:19
  • @Pangea: the link gets javaversion using a batch file, I needed something through which I get javaversion 1.6.0_26 directly using a one liner, since I cannot deploy my batch file in customers machine Commented Sep 30, 2011 at 6:23
  • Why not just call for the java.version property in code and either use that directly or write it to a new blank line in the file? Otherwise I really can't see what Java (as a programming language) has to do with this. Commented Sep 30, 2011 at 8:25

6 Answers 6

4

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 )
Sign up to request clarification or add additional context in comments.

5 Comments

I know its too much to ask but still " cannot be removed?
See my edit. However this assumes a certain length of the response which might not always work well.
Hmm, works for me. Do you have the "Command Extensions" enabled? They are by default and that's what enables the %v:~1,8% syntax
I have no idea what you just said :P what do you mean by "Command Extensions" Also I need to go with whatever comes by default
@AbhishekSimon: see my edit. It seems the first syntax only works under Windows7
3

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

Comments

2

You can try:

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

3 Comments

Bollocs, haven't noticed that earlier
you might want to delete your answer, as people may down vote
Actually, this answer is perfectly valid with a Windows port of the UNIX utils.
1
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).

Comments

0

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

Comments

0

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%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.