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:
Pro
Just a quick bump because i found a better solution than the owner picked answer.
I found that it works with only 32bit Java and today time, thats pretty outdated. Therefor I made a adjustment for 64bit systems. Hope this helps anyone else looking for a way to pull the paths.
private string GetJavaInstallationPath()
{
string environmentPath = Environment.GetEnvironmentVariable("JAVA_HOME");
if (!string.IsNullOrEmpty(environmentPath))
{
return environmentPath;
}
string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment\\";
if (!Environment.Is64BitOperatingSystem)
{
using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(javaKey))
{
string currentVersion = rk.GetValue("CurrentVersion").ToString();
using (Microsoft.Win32.RegistryKey key = rk.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();
}
}
}
else
{
using (var view64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry64))
{
using (var clsid64 = view64.OpenSubKey(javaKey))
{
string currentVersion = clsid64.GetValue("CurrentVersion").ToString();
using (RegistryKey key = clsid64.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();
}
}
}
}
}
You can do it through the registry. You were looking in the wrong place though. I knocked together a quick example for you:
private string GetJavaInstallationPath()
{
string environmentPath = Environment.GetEnvironmentVariable("JAVA_HOME");
if (!string.IsNullOrEmpty(environmentPath))
{
return environmentPath;
}
string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment\\";
using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(javaKey))
{
string currentVersion = rk.GetValue("CurrentVersion").ToString();
using (Microsoft.Win32.RegistryKey key = rk.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();
}
}
}
Then to use it, just do the following:
string installPath = GetJavaInstallationPath();
string filePath = System.IO.Path.Combine(installPath, "bin\\Java.exe");
if (System.IO.File.Exists(filePath))
{
// We have a winner
}
First of all, Do not try to get java JDK from the environment variable use the Registry key which is more effective and will give you anything needed. Here is an example to check if it exists and if it does exist it can return the relative path or the name with the current version. I spent hours to make this work I hope it helps someone. In case it helps 32 bits JDK is being discontinued so I only provided with 64bits solution.
public static string checkInstalled(string findByName)
{
string displayName;
string InstallPath;
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
//64 bits computer
RegistryKey key64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey key = key64.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayName = subkey.GetValue("DisplayName") as string;
if (displayName != null && displayName.Contains(findByName))
{
InstallPath = subkey.GetValue("InstallLocation").ToString();
return InstallPath; //or displayName
}
}
key.Close();
}
return null;
}
you can call this method like this
string JavaPath = Software.checkInstalled("Java(TM) SE Development Kit");
and boom. Cheers
As far as I know the idea is that the latest version of Java installed on the system is the first one found in the PATH environment variable, so you shouldn't need to look for any registry keys, just run the thing.
Try:
ProcessStartInfo info = new ProcessStartInfo("java.exe", "-jar somerandom.jar");
If it doesn't work make sure java.exe is in your path and let me know.
Building on top of @GenericTypeTea question - this is a way how to check both on x32/x64.
static string GetJavaInstallationPath()
{
string environmentPath = Environment.GetEnvironmentVariable("JAVA_HOME");
if (!string.IsNullOrEmpty(environmentPath))
{
return environmentPath;
}
const string JAVA_KEY = "SOFTWARE\\JavaSoft\\Java Runtime Environment\\";
var localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
using (var rk = localKey.OpenSubKey(JAVA_KEY))
{
if (rk != null)
{
string currentVersion = rk.GetValue("CurrentVersion").ToString();
using (var key = rk.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();
}
}
}
localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
using (var rk = localKey.OpenSubKey(JAVA_KEY))
{
if (rk != null)
{
string currentVersion = rk.GetValue("CurrentVersion").ToString();
using (var key = rk.OpenSubKey(currentVersion))
{
return key.GetValue("JavaHome").ToString();
}
}
}
return null;
}