How to determine Windows Java installation location

前端 未结 5 1216
再見小時候
再見小時候 2020-12-03 04:40

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         


        
5条回答
  •  不思量自难忘°
    2020-12-03 05:22

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

提交回复
热议问题