简体   繁体   English

如何检测是否安装了 Java 运行时

[英]How to detect whether Java runtime is installed or not

I program windows applications using Java and this builds a ".jar" file not an ".exe" file.我使用 Java 编写 Windows 应用程序,这会构建一个“.jar”文件而不是“.exe”文件。 When a client computer with no java runtime installed opens the ".jar" file, it runs as an archive with winrar.当没有安装 java 运行时的客户端计算机打开“.jar”文件时,它会作为带有 winrar 的存档运行。 All I want to know is how to detect whether java runtime is installed or not on a computer using c# code in order to show a MessageBox telling user to install java runtime, or launches the ".jar" file using the java runtime if it's installed.我只想知道如何使用 c# 代码检测计算机上是否安装了 java 运行时,以便显示一个 MessageBox,告诉用户安装 java 运行时,或者使用 java 运行时启动“.jar”文件(如果已安装) .

You can check the registry你可以检查注册表

RegistryKey rk = Registry.LocalMachine;
RegistryKey subKey = rk.OpenSubKey("SOFTWARE\\JavaSoft\\Java Runtime Environment");

string currentVerion = subKey.GetValue("CurrentVersion").ToString();

Start 'java -version' in a childprocess.在子进程中启动“java -version”。 Check exitcode and returned output for 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);
    }

You could check in the registry.你可以检查注册表。 This will tell you if you have a JRE, and which version .这将告诉您是否有 JRE,以及哪个版本

From this document :这个文件

HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\<version number>
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Development Kit\<version number>

where the includes the major, minor and the patch version numbers;其中包括主要、次要和补丁版本号; eg, 1.4.2_06例如,1.4.2_06

A small applet in a html page which cancels a redirect to a "Please install Java" page. html 页面中的一个小程序,它取消重定向到“请安装 Java”页面。

EDIT: This is almost the only really bullet-proof way.编辑:这几乎是唯一真正防弹的方法。 Any registry key containing JavaSoft is most likely only for the Sun JVM and not any other (like IBM or BEA).任何包含 JavaSoft 的注册表项很可能仅适用于 Sun JVM,而不适用于任何其他(如 IBM 或 BEA)。

Below code will be of help:下面的代码会有所帮助:

public static bool IsJavaExists()
{
    try
    {
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "java.exe";
        psi.Arguments = " -version";
        psi.RedirectStandardError = true;
        psi.UseShellExecute = false;
        Process pr = Process.Start(psi);
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM