Search code examples
javac#.netregistry

Programmatically check whether the Java Development Kit is installed


As part of an installer, I'm building, I want to check whether the Java Development Kit is installed already (specifically the JDK, the JRE will not do). On my local machine, I can clearly see the key in the registry (HKLM\SOFTWARE\JavaSoft\Java Development Kit), but the following code (C#) doesn't find said key:

RegistryKey jdkKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\JavaSoft\Java Development Kit");

When I investigate which keys are present under "HKLM\SOFTWARE\JavaSoft", using the following code, the Java Development Kit is not there.

string[] keyNames = Registry.LocalMachine.OpenSubKey("SOFTWARE\\JavaSoft").GetSubKeyNames();

I've had a look at my environment variables programmatically, too, and nothing there seems to point to the JDK being present either (it is there, as I said above, it's in the registry, and I'm using it for Java development for the program I'm building an installer for, funnily enough).

Any ideas as to how I can determine whether the JDK is installed? As I say, the JRE alone will not suffice, as I'm doing some compilation at runtime, for which I need the JDK.


Solution

  • It appears I was barking up the wrong (registry) tree. I hadn't realised that the registry root is dependent on the configuration platform of my application. Because my app's target platform was set to x86, all searches began in the Wow6432Node of my registry. Changing my target platform to "Any CPU" fixed this, and I can programmatically access all of the trees of my registry as they appear graphically in regedit.

    It's possibly worth noting that I was using Visual C# 2010 Express to do this, so changing my target platform had to be done by directly editing the XML of my ".csproj" file, as there are no UI options to do this.