tags:

views:

642

answers:

4

I tried googling the answer, but all I found was tips on how to detect Java from a browser or the very generic way of just starting Java and see if it runs, which introduces a possibly long delay in my application. (~ two seconds when started the very first time on my machine)

I hope there is a faster way, if the following restrictions apply:

  • Only Sun JREs or JDKs
  • Only 1.6 and higher
  • Only Windows platforms
  • Not from a browser, but from a plain old Win32 executable

This detection is not meant for a public application, but for internal use on Windows platforms only.

Is there a registry path I can read or some configuration file I can parse?

+4  A: 
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
finnw
Perfect!
DR
+2  A: 

The registry will probably be the easiest route - assuming that an installer has been run. Installed versions can be found in various subkeys under:

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment

If the user has manually configured their environment, you could check JAVA_HOME/walk the PATH variable and check the file version. Demo WSH script:

'file:  whereJava.vbs
'usage: cscript /Nologo whereJava.vbs

'find Java 6 from registry
Set objShell = CreateObject("WScript.Shell")
Wscript.Echo objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\" &_
                   "JavaSoft\Java Runtime Environment\1.6\JavaHome")

'check file version of java.exe
javaHome = objShell.Environment.item("JAVA_HOME")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Wscript.Echo objFSO.GetFileVersion(javaHome & "\bin\java.exe")

See GetFileVersionInfo and company. The major version numbers seem to match the Java version (5, 6). There's a finite amount you can do without invoking the JVM.

McDowell
A: 

Instead you can try running the command "java -version" in command prompt.

This may not actually work well if the JRE is not properly installed but copied from some other machine. A Sure shot workaround is to navigate to the JRE installation directory "C:\Program Files\Java\", navigate to the bin folder from command prompt and then run "java -version". Output will be a installation version, and all relevant information you are looking for.

That's what I want to avoid, see my question.
DR
A: 

There can be any number of installaed JREs and JDKs on a windows machine, but only one will have the HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment set.

You might also consider the "JAVA_HOME" and "Path" environment variables, as they will influence command-line java invocations.

mfx