5

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.

5
  • Last time I checked java installer put java.exe in c:\windows\system32\java.exe. So are you sure you really want to do that? Commented Jun 1, 2010 at 17:27
  • On my machine JRE installs into: c:\Program Files\Java\jre6\bin\java.exe Commented Jun 1, 2010 at 17:32
  • Note that JAVA_HOME is only used by the JDK. Its absence doesn't tell you anything about whether a JRE is installed. And java.exe is part of both ... Commented Jun 1, 2010 at 17:49
  • Ignore JAVA_HOME in my code example. It could be my own env. variable which I want to setup from batch script for future use. Commented Jun 1, 2010 at 18:18
  • Ok. this is what I have got so far: <code> for /f %%j in ("java.exe") do ( set REAL_JAVA_PATH="%%~dp$PATH:j" ) </code> Commented Jun 1, 2010 at 18:19

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I installed JRE7-32bit on Windows 8. reg.exe does not recognize any version of Java. I can run executable jars, but I can't use any java command in the console like java or javaw. How can I detect if Java is installed in such a system?
4

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

1 Comment

On my machine "where java" produces a 32 bit version of java while " java -version" finds a 64 bit version.
1

Most recent versions write to the registry:

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft

You can look for what keys are in there and get out the path using reg.exe

2 Comments

Can I be sure that all java versions on all windows versions will have the same path in registry?
This reg path is probably pretty standard, I just don't know how long JAVA has been using the same path(s)/keys. If you wanted to be absolutely sure, you could distribute the java exe with whatever app you're trying to run.
1

I would (using batch)

::get javaw.exe from the latest properly installed jre
for /f tokens^=2^ delims^=^" %%i in ('reg query HKEY_CLASSES_ROOT\jarfile\shell\open\command /ve') do set JAVAW_PATH=%%i

::if reg entry is not found, java is not installed
if "%JAVAW_PATH%"=="" goto YOUR_ERROR

::then strip "\javaw.exe" from the JAVAW_PATH obtained above
set JAVA_HOME=%JAVAW_PATH:\javaw.exe=%

This should work with jre 1.6 and 1.7 installed in windows xp and seven and be way more faster than searching the filesystem.

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.