views:

515

answers:

3

Is there a standard way to do this? I realize this can be somewhat platform dependent. Our product right now is only supported on Windows - so I suppose that's what I'm interested in right now. The only things I can think of are to either scan the registry or crawl the file system. Scanning the file system seems like it can take a really long time - and the registry can be unreliable. Should I do both? Any other suggestions? I tried to look for an API to do this with no luck.

+1  A: 

I'd probably go for a combination of looking for the Java installed registry keys and crawling the default locations for installation (which shouldn't take too long).

An alternative approach would be to bundle a tiny Java application which prints various details such as the running JVM.

Mike McQuaid
+3  A: 

I would firstly start by looking for the JAVA_HOME environment variable (and possibly JDK_HOME although thats far less common) and then determining what version that is and whether it's a JDK or JRE.

After that check for common instead locations. Find out the system's program files directory (don't just assume it's C:\Program Files even though it is 99.5% of the time) and look for common install locations under that (eg Java).

I wouldn't do an exhaustive search.

It's worth asking: do you really need to find JDKs this way? Can't you just ask the user what JDK he or she wishes to use, possibly suggesting any easy ones you've found already?

cletus
Cletus can you defend why you wouldn't do an exhaustive search? Intuitively I agree here but I'm looking for strong reasons.
Amir Afghani
An exhaustive search would require searching all hard drives for the files java.exe or javac.exe (assuming windows) which could take a lot of time.
Chris Nava
Suggest you limit your search to C:\Program Files\ and the %PATH% environment variable in addition to the two variables listed above.
Chris Nava
A: 
System.out.println(System.getProperty("java.version"));

Other properties here

tulskiy
That only tells the version of the JRE executing the code - not what JDK/JREs are installed
Jonik
Sorry, I misunderstood the question. You can use registry and if nothing was found, ask the user. The key is HKEY_LOCAL_MASHINE/SOFTWARE/JavaSoft/Java Development Kit
tulskiy