How can I detect the installed Sun/Oracle JRE on Windows?

39,542

Solution 1

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment

Solution 2

Windows > Start > cmd >

C:> for %i in (java.exe) do @echo.   %~$PATH:i

If you have a JRE installed, the Path is displayed, for example: C:\Windows\System32\java.exe

Solution 3

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.

Solution 4

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.

Share:
39,542
Daniel Rikowski
Author by

Daniel Rikowski

Updated on December 08, 2020

Comments

  • Daniel Rikowski
    Daniel Rikowski over 3 years

    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/Oracle 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?