问题
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?
回答1:
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.
回答2:
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.
回答3:
Java Class files contain version information. As with any Java version it should not be possible to execute a class file (or jar) that was compiled with a newer major version than your runtime. See: https://stackoverflow.com/a/4692743/6239524
来源:https://stackoverflow.com/questions/43932091/compatibility-and-migration-from-java-9-to-java-8