7

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?

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
  • 1
    If the compability was bidirectional, there wouldn't be need for Java 9 – xenteros May 12 '17 at 07:55
  • 2
    @xenteros Java 9 may be needed to augment the jar with module information (via `module-info.java`) – ZhekaKozlov May 12 '17 at 08:02
  • I don't know what will happen, but you probably know what happens when you try to run an application which is compiled with a newer major version of Java. – Obenland May 12 '17 at 14:32

3 Answers3

10

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.

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
5

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.

BetaRide
  • 16,207
  • 29
  • 99
  • 177
  • 4
    That's not *universally* true. But it's a reasonable guideline. – T.J. Crowder May 12 '17 at 07:49
  • And if I compile the application with a javac compiler targetted, i.e, for Java 8? After I use all the tools of a JDK8, i.e., jar for packaging that application? Maybe is the module descriptor simple ignored? – xdevel2000 May 12 '17 at 07:52
  • I can compile application written in Java 8 with Java 7 compiler if I didn't use any Java 8 features. – xenteros May 12 '17 at 07:57
0

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

Community
  • 1
  • 1
kalsowerus
  • 998
  • 8
  • 23