Java 9/10 and macOS – issues with java.version
It looks like you have to be extra careful while working with recent releases of JDK for macOS.
If you want to get JVM’s version (while running application) there is a simple way of doing it.
public class Simple { public static void main(String [] args) { System.out.println(System.getProperty("java.version")); } }
However, you have to be extra careful in macOS. For JVM 9 and JVM 10 you will get “9” and “10” respectively. This might be an issue, especially when you parse the version String to get major/minor numbers
String version = System.getProperty("java.version"); // this will give you -1 (for JVM 9/10), and that's no good int dotPos = version.indexOf('.');
The funny thing is, that moving to 10.0.2 will suddenly give you different format (proper one).
In fact, there is a completely new way of getting JVM version since release 9 – Runtime.version(). And this one, will give all the information you need.
public class Simple { public static void main(String [] args) { System.out.println(System.getProperty("java.version")); Runtime.Version jvmversion = Runtime.version(); System.out.println("major: " + jvmversion.major() + " minor: " + jvmversion.minor() + " security: " + jvmversion.security()); } }
which will give you
9 major: 9 minor: 0 security: 0
And what is your preferred way of getting Java’s version while running the code?