tags:

views:

240

answers:

3
+1  A: 

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

JamesG
Can I be sure that all java versions on all windows versions will have the same path in registry?
Ma99uS
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.
JamesG
+1  A: 

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).
javamonkey79
+2  A: 

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.

Photodeus