Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compatibility and migration from Java 9 to Java 8

Tags:

java

java-9

I'm reading the document about the new Java 9 module system.

The paragraph 3 Compatibility & migration explains how to migrate code from Java 8 to Java 9, but says nothing on how "migrate" or run an application written in Java 9 to pre Java 9 runtimes.

So, if I have, say a JAR application written in a modularity way (every module has a module descriptor) what does happen if I deploy it on, i.e, a JDK 8 runtime?

like image 697
xdevel2000 Avatar asked May 12 '17 07:05

xdevel2000


People also ask

Is Java 8 backwards compatible?

Backward CompatibilityJava versions are expected to be binary backwards-compatible. For example, JDK 8 can run code compiled by JDK 7 or JDK 6. It is common to see applications leverage this backwards compatibility by using components built by different Java version.

Is JDK 9 backwards compatible?

The JDK compiler is not backward compatible. So code cannot be compiled by java 1.5 to run on 1.4.

What is the difference between Java 8 and Java 9?

Java 8 applications use packages as a top-level component whereas Java 9 applications use modules as a top-level component. Each Java 9 module has only one module with one module descriptor whereas Java 8 package doesn't build multiple modules into a single module.


2 Answers

If your class files are compiled with --release 8 flag, then they should run fine on Java 8. module-info.class files will be ignored by the 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, take a look at JEP 238 (Multi-Release JAR Files). This allows you to take even more advantages of Java 9 features without breaking compatibility with Java 8.

like image 78
ZhekaKozlov Avatar answered Oct 05 '22 19:10

ZhekaKozlov


You cannot start a Java application with a JRE which is less than the one it is built for.

If you just use javac without any special options it will produces classes which do run on JREs equal or bigger than the one of the used JDK.

However javac supports cross compilation. You can use JDK 8 to compile JDK 6 compatible class files. Search for Cross-Compilation Options in the javac docs.

like image 32
BetaRide Avatar answered Oct 05 '22 18:10

BetaRide