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

发布于 2024-08-13 09:45:19 字数 212 浏览 3 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌生 2024-08-20 09:45:19

你可以检查一下注册表

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

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

You can check the registry

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

string currentVerion = subKey.GetValue("CurrentVersion").ToString();
海夕 2024-08-20 09:45:19

在子进程中启动“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);
    }

Start 'java -version' in a childprocess. 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);
    }
乖乖哒 2024-08-20 09:45:19

你可以去注册表查一下。这将告诉您是否有 JRE,以及哪个版本

来自本文档

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

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

You could check in the registry. This will tell you if you have a JRE, and which version.

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

╭ゆ眷念 2024-08-20 09:45:19

html 页面中的一个小程序,用于取消重定向到“请安装 Java”页面。

编辑:这几乎是唯一真正防弹的方法。任何包含 JavaSoft 的注册表项很可能仅适用于 Sun JVM,而不适用于任何其他(如 IBM 或 BEA)。

A small applet in a html page which cancels a redirect to a "Please install Java" page.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文