如何确定Windows Java安装位置 [英] How to determine Windows Java installation location
问题描述
我正在尝试从C#程序集动态运行.jar(使用 Process.Start(info)
)。现在,从控制台应用程序我可以运行:
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");
但是,在程序集中,我不断收到 Win32Exception
系统无法找到指定的文件,并且必须将行更改为Java的完整路径,如下所示:
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");
这显然不行。我需要一种动态(但声明性地)确定Java的安装位置的方法。
This obviously won't do. I need a way to dynamically (but declaratively) determine the installed location of Java.
我开始考虑寻找注册表,但是当我到达那里时我注意到那里是版本的特定键,甚至不能保证它们是数字(例如HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment \1.6和HKEY_LOCAL_MACHINE\SOFTWARE \ JavaSoft \ Java运行时环境 \ 1.6.0_20)。
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").
从一个查找最新的java.exe路径的最可靠的长途解决方案是什么? C#申请?
What would be the most reliable "long-haul" solution to finding the most up-to-date java.exe path from a C# application?
非常感谢提前。
- 编辑 -
感谢 GenericTypeTea 和Stephen Cleary 的答案,我已解决了以下问题:
Thanks to a combination of GenericTypeTea's and Stephen Cleary's answers, I have solved the issue with the following:
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();
}
}
推荐答案
你可以通过注册表完成。你看错了地方。我为你敲了一个简单的例子:
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
}
这篇关于如何确定Windows Java安装位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!