Daniel Rikowski
Daniel Rikowski

Reputation: 72504

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

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:

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?

Upvotes: 16

Views: 40505

Answers (5)

McDowell
McDowell

Reputation: 108859

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.

Upvotes: 4

grokster
grokster

Reputation: 6279

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

Upvotes: 6

mfx
mfx

Reputation: 7388

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.

Upvotes: 1

Shubhranshu
Shubhranshu

Reputation:

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.

Upvotes: 1

finnw
finnw

Reputation: 48619

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment

Upvotes: 20

Related Questions