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

AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • Describe your end goal? Perhaps there is a better way. And isn't this the same question as http://stackoverflow.com/questions/5675459/how-to-get-java-version-from-batch-script – Aravind Yarram Sep 30 '11 at 06: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 – AabinGunz Sep 30 '11 at 06:23
  • Why not just call for the [java.version](http://pscode.org/prop/?prop=java.specification.version%2Cjava.version%2Cjava.vm.specification.version&format=TSV) 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. – Andrew Thompson Sep 30 '11 at 08:25

6 Answers6

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 )
  • I know its too much to ask but still `"` cannot be removed? – AabinGunz Sep 30 '11 at 08:30
  • See my edit. However this assumes a certain length of the response which might not always work well. –  Sep 30 '11 at 09:05
  • 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 –  Sep 30 '11 at 09:42
  • 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 – AabinGunz Sep 30 '11 at 10:39
  • @AbhishekSimon: see my edit. It seems the first syntax only works under Windows7 –  Sep 30 '11 at 15:51
3

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

Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
2

You can try:

java -version 2>&1 | awk '/version/ {print $3}' | egrep -o '[^\"]*'
Kris
  • 5,714
  • 2
  • 27
  • 47
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).

npocmaka
  • 55,367
  • 18
  • 148
  • 187
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%
equiman
  • 7,810
  • 2
  • 45
  • 47
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\""

AabinGunz
  • 12,109
  • 54
  • 146
  • 218