Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Java Version from batch script?

I am trying to get '6' out of the java version output given below

java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)

For the same I wrote this batch script

set VERSION6="1.6.0_21"
java -version 2>&1 | findstr "version" >ab.txt
for /f "tokens=3" %%g in (ab.txt) do (
  if not %%g == %VERSION6% echo %%g
  echo %%g
)

%%g displays "1.6.0_21"

May someone guide me to correct direction? I am not much familiar with for /f.

like image 940
AabinGunz Avatar asked Apr 15 '11 10:04

AabinGunz


People also ask

How do I check java version in cmd?

Check the Java Version Using the Command Line You can also check your Java version in Windows using the command line. First, click on the magnifying glass and type “cmd”, then click on the Command Line app icon that appears. Now, enter the command java -version and you'll see the version of Java listed.

How do you check java is installed or not in batch file?

Open command prompt and enter “java –version”. If installed version number is displayed. 2. On Windows, Java is usually installed in the directory C:/Program Files/Java.

How do I list all java versions in Windows?

Open Command Prompt by pressing Start Key + R. Type cmd and press Enter. Type "java -version" and press Enter.


2 Answers

@echo off
setlocal

set VERSION6="1.6.0_21"
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
    @echo Output: %%g
    set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
@echo Output: %JAVAVER%

for /f "delims=. tokens=1-3" %%v in ("%JAVAVER%") do (
    @echo Major: %%v
    @echo Minor: %%w
    @echo Build: %%x
)

endlocal

In the first for loop, "tokens=3" says that we're going to just use the third token from the command output. Rather than redirect the output of the java -version command to a file, we can run this command within the for loop itself. The carets (^) are escape characters, and are needed so we can embed the >, & and | symbols in the command string.

Within the body of the for loop, we set a new var, JAVAVER, so that we can do some manipulation of the version string later.

The set JAVAVER=%JAVAVER:"=% command removes the double quotes from around the version string.

The last for loop parses the java version string. delims=. says we're going to delimit tokens using periods. tokens=1-3 says we're going to pass the first three tokens from the string to the body of the loop. We can now get the components of the java version string using the explicit variable, %%v and the implied variables (next letters in the alphabet) %%w and %%x.

When I run this on my system I get:

Output: "1.6.0_24" 
Output: 1.6.0_24
Major: 1 
Minor: 6 
Build: 0_24
like image 96
Patrick Cuff Avatar answered Sep 30 '22 18:09

Patrick Cuff


This will extract the minor part of the version number:

java -version 2>&1 | awk '/version/ {print $3}' | awk -F . '{print $2}'

However, it may be better to extract the major.minor and match on that in case Oracle ever change the version number scheme again e.g.:

java -version 2>&1 | awk '/version/ {print $3}' | egrep -o '[0-9]+\.[0-9]+'
like image 26
Owen Fraser-Green Avatar answered Sep 30 '22 18:09

Owen Fraser-Green