How to detect whether Java runtime is installed or not

后端 未结 4 566
遇见更好的自我
遇见更好的自我 2021-01-02 06:12

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

相关标签:
4条回答
  • 2021-01-02 06:19

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

    0 讨论(0)
  • 2021-01-02 06:30

    You can check the registry

    RegistryKey rk = Registry.LocalMachine;
    RegistryKey subKey = rk.OpenSubKey("SOFTWARE\\JavaSoft\\Java Runtime Environment");
    
    string currentVerion = subKey.GetValue("CurrentVersion").ToString();
    
    0 讨论(0)
  • 2021-01-02 06:37

    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);
        }
    
    0 讨论(0)
  • 2021-01-02 06:38

    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

    0 讨论(0)
提交回复
热议问题