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

5

16

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?

Ensign answered 27/8, 2009 at 9:24 Comment(2)
Do you see a delay if the JRE is not installed? And if it is installed aren't you going to run the program and accept the startup delay anyway?Behring
Not immediately. The application has many features and on startup all prerequisites are checked. Sometimes the user doesn't even get to the code path requiring Java, but still has to suffer from the delay. The users already frown upon the long startup...Ensign
B
20
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
Behring answered 27/8, 2009 at 9:26 Comment(4)
or HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime EnvironmentRibbing
@ParchedSquid, the OP stated "from a plain old Win32 executable", which will not see the Wow6432Node.Behring
On my PC HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment point to an old uninstalled JRE. The only correct registration for me is under HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime EnvironmentEmancipated
Considering the numerous first time users, it might be useful to add this piece of info. to bring up the required window: type regedit in the run promptInsalivate
F
6

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

Fellner answered 9/5, 2012 at 22:57 Comment(1)
This is certainly a naive solution.Aventurine
F
4

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.

Frog answered 27/8, 2009 at 10:29 Comment(0)
S
1

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.

Senior answered 27/8, 2009 at 10:49 Comment(1)
That's what I want to avoid, see my question.Ensign
D
1

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.

Daytoday answered 4/9, 2009 at 8:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.