Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 397
AabinGunz Avatar asked Jan 18 '23 15:01

AabinGunz


2 Answers

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 )
like image 159
a_horse_with_no_name Avatar answered Jan 21 '23 05:01

a_horse_with_no_name


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

like image 40
Muhammad Aamir Ali Avatar answered Jan 21 '23 03:01

Muhammad Aamir Ali