How to detect presence and location of JVM on Windows?

31,772

Solution 1

I'm going to throw my hat in the ring with the code I've ended up using:

string javaDirectory = null;

// Native registry key - 32b on 32b or 64b on 64b
// Fall back on 32b Java on Win64 if available
RegistryKey javaKey =
    Registry.LocalMachine.OpenSubKey("SOFTWARE\\Javasoft\\Java Runtime Environment") ??
    Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Javasoft\\Java Runtime Environment");

if (javaKey != null)
{
    string javaVersion = javaKey.GetValue("CurrentVersion").ToString();
    try
    {
        javaDirectory = javaKey.OpenSubKey(javaVersion).GetValue("JavaHome").ToString();
    } catch(NullReferenceException)
    { /* Ignore null deref, means we can't get a directory */ }
}

if (javaDirectory == null)
{
    // deal with a lack of Java here.
}

Solution 2

A properly installed JVM on a windows system will (most likely..) respond to the command shell command:

java -version

This does not return the path, but try

java -verbose -version

(At least) one of the lines will contains the substring rt.jar and this line contains the path of the "active" java virtual machine.

Share:
31,772
Matthew Scharley
Author by

Matthew Scharley

Email: [email protected]

Updated on July 14, 2022

Comments

  • Matthew Scharley
    Matthew Scharley almost 2 years

    I'm trying to detect if there is a JVM installed and where it is located so I can run java.exe.

    All I've managed to find is HKCU\Software\JavaSoft\Java Runtime Environment\<ver>. Is it safe to assume that it is installed in %PROGRAMFILES%\Java\jre<ver>?

    I'm trying to do this in C#, but I assume the answer is pretty language agnostic, so any answer is appreciated.

    EDIT: Ok silly me, I found How to detect whether java runtime is installed or not on a computer using c# which pointed me at HKLM\Software\JavaSoft\Java Runtime Environment\CurrentVersion which works with HKLM\Software\JavaSoft\Java Runtime Environment\<ver>\JavaHome. I managed to find these instead underneath HKLM\Software\Wow6432Node\JavaSoft\Java Runtime Environment. Is there some way to detect which of these I should be checking without trying to sniff at the CPU type?

  • Christian
    Christian over 13 years
    Either that, or the registry entry for jars should be set. There's a WinAPI function to get file associations, but I forgot about it.
  • Matthew Scharley
    Matthew Scharley over 13 years
    On my dev pc at least, this is not true. java isn't registered in PATH. In general, I think relying on PATH on Windows is a bad idea, simply because so many apps don't bother with it.
  • Matthew Scharley
    Matthew Scharley over 13 years
    @Christian file associations aren't reliable either. Some archive managers overwrite the jar association.
  • Christian
    Christian over 13 years
    @Matthew Scharley - I agree, but it's still a possible further check.
  • Matthew Scharley
    Matthew Scharley over 13 years
    @Andreas that said, you are technically correct, and I've seen this repeated a lot on the internet when looking for an answer to this. But from personal experience, I've needed to tweak PATH for quite a few apps that were supposed to set it up properly.
  • Matthew Scharley
    Matthew Scharley over 13 years
    Also unfortunately not setup properly on my dev PC. Definitely worth a first shot attempt though. Can you include an example output for my benefit please?
  • Taber
    Taber over 13 years
    I'm looking at the environment variables of a PC with newly installed Java JRE. I have not found the varialbe "JAVA_HOME" (my mistake, sorry) but if a so-called "CLASSPATH" that can be helpful.
  • Andreas Dolk
    Andreas Dolk over 13 years
    @Matthew - A java installation on windows drops some java.exe into the windows (or ./system32) folder. I don't have a special entry on my PATH environment - but it works as described above.
  • Matthew Scharley
    Matthew Scharley over 13 years
    @Andreas Now I'm curious. Do you have the JRE or the JDK? I have the Oracle JRE downloaded from java.com . It's entirely possible that we have two different JRE's and we are both correct.
  • Andreas Dolk
    Andreas Dolk over 13 years
    @Matthew - java -version tells JRE 1.6.0_21, it has been installed through the standard oracle JDK (!) installer and I have no PATH entry pointing to the install directory. javac can't be resolved from the console, the java call executed C:\WINDOWS\system32\java.exe
  • Matthew Scharley
    Matthew Scharley over 13 years
    It didn't on my PC, not anywhere in PATH. (win7 64)
  • Matthew Scharley
    Matthew Scharley over 13 years
    This just prompted me to do a search on my C:\ , found it in C:\Windows\SysWOW64. Must be an analogous idea to HKLM\Software\Wow6432Node. IMO, looks like MS seriously screwed up x86 compatibility...
  • Rich Oliver
    Rich Oliver about 12 years
    @MatthewScharley Ah thankyou, lets see if I can finally get eclipse to run.