如何确定 Windows Java 安装位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3038140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine Windows Java installation location
提问by Lance
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:
我正在尝试从 C# 程序集(使用Process.Start(info)
)动态运行 .jar 。现在,我可以从控制台应用程序运行:
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:
但是,在程序集中,我不断收到Win32Exception
“系统找不到指定的文件”的消息,并且必须将行更改为 Java 的完整路径,如下所示:
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.
这显然不行。我需要一种方法来动态(但以声明方式)确定 Java 的安装位置。
I started thinking of looking to the registry, but when I got 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").
我开始考虑查看注册表,但是当我到达那里时,我注意到版本有特定的键,甚至不能保证它们是数字(例如“HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.6”和“HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java 运行时环境\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?
从 C# 应用程序中找到最新的 java.exe 路径的最可靠的“长途”解决方案是什么?
Thanks much in advance.
非常感谢。
- EDIT -
- 编辑 -
Thanks to a combination of GenericTypeTea's and Stephen Cleary's answers, I have solved the issue with the following:
多亏了GenericTypeTea和Stephen Cleary的回答,我已经解决了以下问题:
private String GetJavaInstallationPath()
{
String javaKey = "SOFTWARE\JavaSoft\Java Runtime Environment";
using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(javaKey))
{
String currentVersion = baseKey.GetValue("CurrentVersion").ToString();
using (var homeKey = baseKey.OpenSubKey(currentVersion))
return homeKey.GetValue("JavaHome").ToString();
}
}
回答by djdd87
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
}
回答by Grzenio
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.
据我所知,系统上安装的最新版本的 Java 是在 PATH 环境变量中找到的第一个版本,因此您不需要查找任何注册表项,只需运行该东西即可。
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.
如果它不起作用,请确保 java.exe 在您的路径中并告诉我。
回答by Ondrej Svejdar
Building on top of @GenericTypeTea question - this is a way how to check both on x32/x64.
建立在@GenericTypeTea 问题之上 - 这是一种如何在 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;
}
回答by Jamie
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.
我发现它只适用于 32 位 Java,而今天,这已经过时了。为此,我对 64 位系统进行了调整。希望这可以帮助其他任何人寻找一种拉路径的方法。
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();
}
}
}
}
}