How to determine Windows Java installation location

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;
}

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
}

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();
                        }
                    }
                }
            }

        }

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.