How to find the supported backward compatibility versions of java?

325 Views Asked by At

Is it possible to find all the supported backward compatible Java versions supported by currently installed JDK through command line?

I was going through command line options, but I couldn't find any option which seems to be providing this information.

This requirement came because there are two different machines where an application need to be run. In one machine the JDK 11 is installed where JAXB is not supported, where as in another machine JDK 8 is installed which supports JAXB.

Update: Based on response "whatever it means", I would like to expand what is being expected:

$ java <command_line_option> 
JDK11, JDK10, JDK9, JDK8
2

There are 2 best solutions below

0
Pino On

Generally speaking, new Java versions are compatible with old ones but with some caveats, so that hypothetical command-line option should list all previous versions (or none!); however I confirm what you have already seen: that command line option does not exist.

Instead of searching for a "backward compatible Java version", just adapt the application to work both with Java 8 and Java 11: it does not require much time and it will probably work with Java 17 and Java 21 too.

Java 11 can run code compiled with Java 8 but, as you already know, some libraries have been removed from the JDK; so, to run a Java 8 application on Java 11 too, the first thing to do is to add the missing libraries to its classpath. If you have a fat jar you can add these libraries in the fat jar (they are harmless if you run on Java 8). Another thing that could be necessary is opening some modules, for example with this command line option: --add-opens java.base/java.lang=ALL-UNNAMED.

0
Stephen C On

What you are asking for does not exist, and (in the general sense) is not practical, at least for versions of Java after Java 8. If you treat a Java application as black box (apart from its classfile version numbers) you cannot predict whether it will work on any versions of Java after the versions it has been testified (and certified) on.

So you need to make it the application writer's problem. Have the application include a simple launcher that tests to see what version of Java it is running on. If the version is too new (or too old) the launcher should exit with an error message. If the Java version is acceptable, and other environmental dependencies and preconditions are satisfied, the launcher can then dynamically load the application.

Note the launcher needs to be application specific. But in the simple case it just has a list of supported Java major version numbers ... provided by the application writer, after (presumably) thorough testing on all available Java versions.

You can implement this even if you are not the application writer. It would be simple to write a general purpose version checking launcher, and package it in a JAR with the application JAR, a JAR-in-JAR class loader and a resource that lists the supported Java versions. Or you could just implement it as a shell script or batch file. (That is the normal way it is done ...)


Aside: your application probably should not aim to support non-LTS versions like Java 9 and 10. These only had a short lifetime and have been superseded. (Indeed, after Java 11 LTS, the non-LTS versions only have 6 months of support before they are "end of life".) Patches are not available for these versions, even under an Oracle support contract (AFAIK).