View all available java versions on the machine

advertisements

I'm writing an eclipse plug-in and I need to display all versions of Java installed on users machine. I know about this code:

String javaVersion = System.getProperty("java.version");

but it only returns currently selected Java version. How would I get them all?


This is much more complicated then you think.

Of course, when you run some java class; that already runs inside a single JVM. And that JVM installation doesn't have any idea if/where other JVMs might exist on your system.

Therefore it depends on your operating system where other JVMs might be located.

On a Linux system, you could go for

find / -name "java" -type f 2>/dev/null

On my system, that creates output like:

/usr/lib/j2re1.7-ibm/jre/bin/java
/usr/lib/j2sdk1.7-ibm/jre/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
/usr/lib/jvm/java-ibm-x86_64-80/jre/bin/java
/usr/share/bash-completion/completions/java
/data/home/Downloads/ibm-java-x86_64-80/jre/bin/java
...

That is the whole point: a user can place JVM binaries all over his file system. And he could be starting a JVM within all those different directories.

So the real answer here: step back; and look at the requirement behind your question. Why do you think you need to know about this? And from there: how can you get rid of the requirement?