tags:

views:

256

answers:

6

Hi. 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?

+1  A: 

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.

DrJokepu
Thanks.. guess I'll have to keep looking :) You have any technique to check if jre is installed from any code or command promp?
Johannes
As David said, the only "bulletproof" way is by running a small Java program. Also, you can try parsing the output of "java -version". It wouldn't be 100% idiot-safe, but I suppose it would be good enough.
DrJokepu
+2  A: 

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.

David Zaslavsky
Yeah that sounds like a reasonable technique.
DrJokepu
Thanks :) That will work!
Johannes
A: 

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.

Tetsujin no Oni
+1  A: 

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.

Thorbjørn Ravn Andersen
A: 

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;
}
rodey
+2  A: 

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.

Rich Kroll