Of course, it depends on the purpose of the operation. If you want to tell the user about this in a Java-compatible marketing, you will have to face the fact that the marketing itself has never used consistent labeling and can retroactively relate older versions.
If the check is intended only to ensure the presence of certain functions or bug fixes, it is enough to assign an ascending number for each version. You can then assign 0 to Java 1.0, which is significantly different from Java 1.1 (assign it 1 ) and get consistent numbering to nine using
public static int getMajorVersion() { String version = System.getProperty("java.class.version"); int p = version.indexOf('.'); if(p>0) version = version.substring(0, p); return Integer.parseInt(version)-44; }
The good thing about the version of a class file is that it is tied to a more formal definition, since it must fit into the two fields of the class file, so it cannot be subject to schema changes or retroactive overrides. In addition, there is no room for prose such as beta, final, or interpreted differently in these two versions. The only thing that protects the code above is the potential omission of the minor .0 , which was not used with Java 1.1, since the version of the main class file was increased for each version.
Of course, there is no guarantee that the number will increase again in each of the next releases, but this is not a problem for compatibility checks, since it will always have at least the previous version number, interpreted as “compatible with the previous release”. To start using the new features, you should still touch the source code. In this case, you can add the Runtime.version() operation for these future releases ...
But note that you get this for free when compiling with --release or --release , since the required minimum version is written to the class file anyway, and older versions of the JVM refuse to execute your code. If you want to optionally support the functions of a version later than the minimum version, you still have to access them dynamically, so in this case, you can just make a reflexive attempt to use this function, go to the backup code if it failed, and you don’t need to do additional verification of the version number. This is exactly what you are doing when trying to implement getMajorVersion() by making reflective Runtime.version().major() , without checking on the previous version for the presence of this function.