我使用Java编写Windows应用程序,这会构建一个".jar"文件,而不是".exe"文件.当没有安装Java运行时的客户端计算机打开".jar"文件时,它将作为带有winrar的存档运行.我想知道的是如何在使用c#代码的计算机上检测是否安装了Java运行时,以便显示MessageBox告诉用户安装java运行时,或者如果已安装则使用java运行时启动".jar"文件.
小智 10
您可以检查注册表
RegistryKey rk = Registry.LocalMachine;
RegistryKey subKey = rk.OpenSubKey("SOFTWARE\\JavaSoft\\Java Runtime Environment");
string currentVerion = subKey.GetValue("CurrentVersion").ToString();
Run Code Online (Sandbox Code Playgroud)
在子进程中启动'java -version'.检查exitcode并返回versioninfo的输出
List<String> output = new List<string>();
private bool checkIfJavaIsInstalled()
{
bool ok = false;
Process process = new Process();
try
{
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.Arguments = "/c \"" + "java -version " + "\"";
process.OutputDataReceived += new DataReceivedEventHandler((s, e) =>
{
if (e.Data != null)
{
output.Add((string) e.Data);
}
});
process.ErrorDataReceived += new DataReceivedEventHandler((s, e) =>
{
if (e.Data != null)
{
output.Add((String) e.Data);
}
});
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
ok = (process.ExitCode == 0);
}
catch
{
}
return (ok);
}
Run Code Online (Sandbox Code Playgroud)
您可以签入注册表。这将告诉您是否具有JRE,以及哪个版本。
从此文件:
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\<version number>
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit\<version number>
Run Code Online (Sandbox Code Playgroud)
其中包括主要版本,次要版本和补丁版本号;例如1.4.2_06