tags:

views:

256

answers:

4

I'm writing a complex setup/installer application in native C++/MFC. I would very much like to be able to detect the version of Java that is installed (if any).

Is this possible, and so, how?

+2  A: 

Fork a process that executes the following command: java -version. Collect the output and parse it. It looks something like the following:

java version "1.5.0_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)
S.Lott
I'll add that you probably need the assistance of a shell on the platform, or else executing 'java' will do nothing since there is presumably no 'java' binary in your current directory.On *nix you would execute something like "sh -c 'java -version'" to make sure you actually execute java from the path. I am not sure of the equivalent on Windows.
Sean Owen
@Sean Owen: Windows normally automatically looks in the process' PATH environment variable, without the assistance of any shell.
Greg Hewgill
+1  A: 

You could try running java -version in a subprocess (reading that process's output with pipe) and parsing the results (if any); or, you could mess with Windows' registry (which feels even more complicated, but may be less kludgy).

Alex Martelli
I managed to find and parse the version information in SOFTWARE\JavaSoft\Java Runtime Environment\BrowserJavaVersionThank you
Would a "launch Java DLL" from within the C++ program be feasible? Should give a good idea what the default JVM is on the machine.
Thorbjørn Ravn Andersen
Browser version could not be the real system version...I would suggest you use @emg-2 answer instead.
Valentin Rocher
+1  A: 

Keep in mind also that it is entirely possible to have more than one JRE installed on a machine at the same time. If your install program detects more than one JRE, it should offer a choice to the user rather than assuming that one of them (eg. the latest one) should be the one which will be used by your app.

Greg Hewgill
+1  A: 

I would suggest to use the registry. You're programming windows, so do it windows-way. Registry-anxiety is irrational.

Registry key for the version:

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\CurrentVersion

Information how to get the value from the registry can be found here.

Information about other java related registry keys can be found here.