Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Difference of way to get Java version at runtime

# JDK Way to launch SourceVersion.latestSupported() Runtime.class.getPackage().getImplementationVersion() Runtime.version() System.getProperty("java.version") System.getProperty("java.specification.version")
1 JDK8 java RELEASE_8 1.8.0_312 java.lang.NoSuchMethodError: java.lang.Runtime.version()Ljava/lang/Runtime$Version; 1.8.0_312 1.8
2 JDK9 java RELEASE_9 null 9.0.4+11 9.0.4 9
3 JDK10 java RELEASE_10 null 10.0.2+13 10.0.2 10
4 JDK11 java RELEASE_11 null 11.0.15+10-Ubuntu-0ubuntu0.20.04.1 11.0.15 11
5 JDK11 java --source 8 RELEASE_11 null org/example/Main.java:10: error: cannot find symbol 11.0.15 11
6 JDK17 java RELEASE_17 null 17.0.3+7-Ubuntu-0ubuntu0.20.04.1 17.0.3 17
7 JDK17 java --source 8 RELEASE_17 null org/example/Main.java:10: error: cannot find symbol 17.0.3 17
8 JDK17 java --source 11 RELEASE_17 null 17.0.3+7-Ubuntu-0ubuntu0.20.04.1 17.0.3 17
  • Runtime.version is not available in CodeChef.
  • SourceVersion is not available in many online IDEs like ideone, paiza.io, Tech.io, repl.it, JDoodle, sololearn, GeeksforGeeks.
  • All these properties are available in Wandbox.
  • Note that System.getProperty("java.vm.version") or ManagementFactory.getRuntimeMXBean().getVmVersion() are not suitable to get the version of Java runtime. It returns the VM build number rather than the version of JDK in some environments. For example, it returns 25.312-b07 in OpenJDK 1.8.0_312 (OpenJDK 64-Bit Server VM (build 25.312-b07)).