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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1855937/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:15:29  来源:igfitidea点击:

How to detect whether Java runtime is installed or not

c#javaruntime

提问by Hazem

I program windows applications using Java and this builds a ".jar" file not an ".exe" file. When a client computer with no java runtime installed opens the ".jar" file, it runs as an archive with 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.

我使用 Java 编写 Windows 应用程序,这会构建一个“.jar”文件而不是“.exe”文件。当未安装 java 运行时的客户端计算机打开“.jar”文件时,它会作为带有 winrar 的存档运行。我只想知道如何使用 c# 代码检测计算机上是否安装了 java 运行时,以显示 MessageBox 告诉用户安装 java 运行时,或者如果安装了 java 运行时则使用 java 运行时启动“.jar”文件.

回答by h4rp0

You can check the registry

您可以检查注册表

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

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

回答by user1536307

Start 'java -version' in a childprocess. Check exitcode and returned output for versioninfo

在子进程中启动“java -version”。检查退出代码并返回版本信息的输出

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

回答by Brian Agnew

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; e.g., 1.4.2_06

其中包括主要、次要和补丁版本号;例如,1.4.2_06

回答by Thorbj?rn Ravn Andersen

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)。