5

I am using sun.misc.Unsafe in my Java8 codebase. This does not work on Java9.

I would like to have the fix for Java9, but with the same codebase that runs on Java8. If I put module-info.java, it will not work, as my codebase is Java8.

What to do?

Naman
  • 27,789
  • 26
  • 218
  • 353
igr
  • 10,199
  • 13
  • 65
  • 111
  • 1
    Buy an expensive support contract from Oracle (and/or) submit a bug report. – Elliott Frisch Dec 02 '17 at 23:33
  • Try `java --add-modules jdk.unsupported ...` when executing under Java 9. – Andreas Dec 02 '17 at 23:37
  • @Andreas Unfortunately, I can't force users to use this flag (it's the library what I have). – igr Dec 02 '17 at 23:45
  • 3
    See [JEP 238: Multi-Release JAR Files](http://openjdk.java.net/jeps/238). – Andreas Dec 02 '17 at 23:50
  • 6
    The question needs to say what "does not work" means. Aside from a one of two small differences, it should work the same in JDK 9 as it did in JDK 8. See JEP 260 (http://openjdk.java.net/jeps/260) for more on this. – Alan Bateman Dec 03 '17 at 08:37

1 Answers1

3

Andreas comment points in the correct direction, you can make use of the Multi-Release JAR Files.

You can create a packaging such that the classes with code common to both the Java JDKs are in the root JAR while the one for which you need to overwrite the implementation is in both the root jar as well as META-INF/versions/9. As noted from the JEP:-

  • In a JDK that does not support MRJARs, only the classes and resources in the root directory will be visible.

  • In a JDK that does support MRJARs, the directories corresponding to any later Java platform release would be ignored; it would search for classes and resources first in the Java platform-specific directory corresponding to the currently-running major Java platform release version, then search those for lower versions, and finally the JAR root.

  • For example, on a Java 9 JDK, it would be as if there were a JAR-specific classpath containing first the version 9 files, and then the JAR root; on a Java 8 JDK, this classpath would contain only the JAR root.

Edit:- Here is a sample project for a similar packaging created with the help of a JetBrains blog using IntelliJ2017.3.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • 6
    MR JARs are indeed useful when you have different code for >= JDK 9. However, it's not clear that it is needed here. If the poster has a library that uses sun.misc.Unsafe then it should work on the class path with JDK 9, exactly as it did with JDK 8 and older. If the poster is creating a module then it will `requires jdk.unsupported` and it should work on the module path (or on the class path where the module-info.class will be ignored). – Alan Bateman Dec 03 '17 at 09:36