Question

Sorry my newbie question :P If I promp "java -version" in the cmd on a windows system, am I guaranteed that the system will be able to run .jar files if I don't get any error?

Was it helpful?

Solution

I guess the only guaranteed way to check for a JRE is to try to run a small Java program.

Or maybe not even that - I suppose conceivably a system could have only part of the Java standard library installed, in which case a small test JAR might work fine but a full program might not. Although I can't imagine why anyone would go to the trouble of setting a system up that way.

OTHER TIPS

From the command line you should be able to invoke "java --version" which will return an error if java is not installed or the currently installed version information.

Well, obviously not. You can put an empty file called java.bat anywhare in PATH, like C:\Windows\System32. Invoking "java" will not yield any errors but it doesn't mean there's a JRE installed.

Why not run a small class file, which write a value to a file which you then check? If it fails, it doesn't work.

A good value might be the value of the java.version system property.

On Windows, you can check the registry at HKLM\SOFTWARE\JavaSoft\Java Plug-in. From there, each subkey is an installed JRE.

edit Here is C# code that will return an array of strings with the installed JRE's

public string[] GetInstalledJavas() {
        // hold the registry subkeys that list the installed JRE's
        string[] jres = null;
        try {
            RegistryKey myKey = Registry.LocalMachine;
            myKey = myKey.OpenSubKey(@"SOFTWARE\JavaSoft\Java Plug-in"); // read-only
            jres = myKey.GetSubKeyNames();
        } catch (Exception myException) {
            Console.Writeline(myException.ToString());
        }
        return jres;
}

I'd actually suggest, if you're only concerned about checking on windows machines, checking the registry for a handler for JNLP... that should guarantee the presence of a relatively recent JRE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top