user89862
user89862

Reputation:

An easy bulletproof technique to check if the system has jre (windows)

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?

Upvotes: 0

Views: 720

Answers (6)

Rich Kroll
Rich Kroll

Reputation: 4005

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.

Upvotes: 2

Ryan Rodemoyer
Ryan Rodemoyer

Reputation: 5682

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

Upvotes: 1

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.

Upvotes: 1

Tetsujin no Oni
Tetsujin no Oni

Reputation: 7367

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.

Upvotes: 0

David Z
David Z

Reputation: 131550

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.

Upvotes: 1

Tamas Czinege
Tamas Czinege

Reputation: 121294

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.

Upvotes: 0

Related Questions