Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check Java installation from batch script?

I need to write a batch script to find out if Java is installed, and if it is, then under what path? I feel it has to be something similar to this:

for /f %%j in ("java.exe") do (
   set JAVA_HOME=..........
)

but I can not figure it out.

P.S. It has to work with path with spaces two. Like if java is installed into "Program Files".

Thanks.

like image 610
Ma99uS Avatar asked Jun 01 '10 17:06

Ma99uS


People also ask

How do you check if Java is correctly installed?

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 know where Java is installed from cmd?

Open a Command Prompt window (Win⊞ + R, type cmd, hit Enter). Enter the command echo %JAVA_HOME% . This should output the path to your Java installation folder.

How do I check Java version in cmd?

Check the Java Version 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 I check my version of Java?

Command Prompt Open CMD and search java -version.


2 Answers

Using reg[.exe] you can query possible JRE candidates that are installed on the system. There could be none or could be several.

On a test set-up, running inside the command shell:

reg query "HKLM\Software\JavaSoft\Java Runtime Environment"

I get three result lines, of which the first is CurrentVersion REG_SZ 1.6

Based on that, querying

reg query "HKLM\Software\JavaSoft\Java Runtime Environment\1.6\"

Gives me JavaHome REG_SZ C:\Program Files\Java\jre6

It's much more efficient than scan a file system to find a java binary.

This was tested under a virtual installation of Windows XP 32-bit.

like image 111
Photodeus Avatar answered Oct 27 '22 23:10

Photodeus


Couldn't you use the 'where' command? As in:

>where java

And test against this?

Example:

C:\Users\myname>where java
C:\Program Files (x86)\Java\jdk1.6.0_17\bin\java.exe

C:\Users\myname>where foo
INFO: Could not find files for the given pattern(s).
like image 39
javamonkey79 Avatar answered Oct 27 '22 22:10

javamonkey79