Java 9 to Java 8 compatibility and migration

I am reading a document about the new Java 9 modular system.

Section 3, "Compatibility and Migration", explains how to port code from Java 8 to Java 9, but does not say anything about how to "migrate" or run an application written in Java 9 to pre-Java 9.

So, if I have, say, a JAR application written in a modular fashion (each module has a module descriptor), what happens if I deploy it, i.e. JDK 8 runtime?

+3


source to share


3 answers


You cannot run a Java application with a JRE that is smaller than the one it was built for.

If you just use javac

without any special options, it will create classes that run on a JRE equal to or greater than one of the JDKs used.



However javac does support cross-compilation. You can use JDK 8 to compile JDK 6 compatible class files. Find the cross compilation options in the javac docs .

+4


source


If your class files are compiled with the flag --release 8

then they should work fine in Java 8. module-info.class

files will be ignored by older JVMs.

If your Java 8 project is maven based, you can easily configure it to include module-info.java

: https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html



Also, have a look at JEP 238 (Multi-Release JAR Files). This allows you to take advantage of Java 9 features even more without breaking Java 8 compatibility.

+6


source


Java class files contain version information. As with any version of Java, it should not be possible to execute a class file (or jar) that was compiled with a newer major version than your runtime. See: fooobar.com/questions/102165 / ...

0


source







All Articles