How to detect presence and location of JVM on Windows?
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.
Comments
-
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 withHKLM\Software\JavaSoft\Java Runtime Environment\<ver>\JavaHome
. I managed to find these instead underneathHKLM\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 over 13 yearsEither 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 over 13 yearsOn my dev pc at least, this is not true.
java
isn't registered inPATH
. In general, I think relying onPATH
on Windows is a bad idea, simply because so many apps don't bother with it. -
Matthew Scharley over 13 years@Christian file associations aren't reliable either. Some archive managers overwrite the jar association.
-
Christian over 13 years@Matthew Scharley - I agree, but it's still a possible further check.
-
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 over 13 yearsAlso 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 over 13 yearsI'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 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 myPATH
environment - but it works as described above. -
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 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, thejava
call executedC:\WINDOWS\system32\java.exe
-
Matthew Scharley over 13 yearsIt didn't on my PC, not anywhere in PATH. (win7 64)
-
Matthew Scharley over 13 yearsThis just prompted me to do a search on my
C:\
, found it inC:\Windows\SysWOW64
. Must be an analogous idea toHKLM\Software\Wow6432Node
. IMO, looks like MS seriously screwed up x86 compatibility... -
Rich Oliver about 12 years@MatthewScharley Ah thankyou, lets see if I can finally get eclipse to run.