How to determine Windows Java installation location
- by Lance May
I'm trying to dynamically run a .jar from a C# assembly (using Process.Start(info)). Now, from a console application I am able to just run:
ProcessStartInfo info = new ProcessStartInfo("java", "-jar somerandom.jar");
In an assembly, however, I keep getting a Win32Exception of "The system cannot find the file specified" and have to change the line to the full path of Java like so:
ProcessStartInfo info = new ProcessStartInfo("C:\\Program Files\\Java\\jre6\\bin\\java.exe", "-jar somerandom.jar");
This obviously won't do. I need a way to dynamically (but declaratively) determine the installed location of Java.
I started thinking of looking to the registry, but when I go there I noticed that there were specific keys for the versions and that they could not even be guaranteed to be numeric (e.g. "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.6" and "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.6.0_20").
What would be the most reliable "long-haul" solution to finding the most up-to-date java.exe path from a C# application?
Thanks much in advance.