With Java 9 modules coming to us soon, you want your existing code to be fully ready for the module system. Making code modular can be a daunting task, but Java 9 comes with a number features to ease migration. This includes automatic modules, the unnamed module and a number of command line arguments.
In this talk we will look at examples of migrating real code. It discusses common problems youll run into during migration, leading to practical tips and the ability to set realistic goals. Its also a good way to understand the module system itself and the various migration paths it supports. This talk is an excellent preparation to start migrating your own code.
* Understanding modules and the module path
* Automatic modules
* Mixing classpath and modulepath
* Dealing with reflection
* Escape switches
* Jdeps
All topics will be based on examples of often used libraries and frameworks.
9. Missing platform modules
error: package javax.xml.bind does not exist
import javax.xml.bind.DatatypeConverter;
public class Main {
public static void main(String... args) {
DatatypeConverter.parseBase64Binary("SGVsbG8gd29ybGQh");
}
}
15. ‣ Classpath compile-time
“java.se” module is used
‣ This does not resolve:
java.activation
java.corba
java.transaction
java.xml.bind
java.xml.ws
java.xml.ws.annotation
Missing platform modules
16. Add unresolved platform modules
jdeps demo/Main.class
Main.class -> java.base
Main.class -> not found
<unnamed> -> java.lang java.base
<unnamed> -> javax.xml.bind not found
Find
17. javac --add-modules java.xml.bind demo/Main.java
Add unresolved platform modules
jdeps demo/Main.class
Main.class -> java.base
Main.class -> not found
<unnamed> -> java.lang java.base
<unnamed> -> javax.xml.bind not found
Find
Fix
18. javac --add-modules java.xml.bind demo/Main.java
Add unresolved platform modules
java --add-modules java.xml.bind demo/Main.java
jdeps demo/Main.class
Main.class -> java.base
Main.class -> not found
<unnamed> -> java.lang java.base
<unnamed> -> javax.xml.bind not found
Find
Fix
20. public class Main {
public static void main(String... args) throws Exception {
sun.security.x509.X500Name name =
new sun.security.x509.X500Name("CN=user");
}
}
Using encapsulated APIs
21. public class Main {
public static void main(String... args) throws Exception {
sun.security.x509.X500Name name =
new sun.security.x509.X500Name("CN=user");
}
}
Using encapsulated APIs
Main.java:4: error: package
sun.security.x509 does not exist
sun.security.x509.X500Name name =
^
22. jdeps -jdkinternals Main.class
Main.class -> java.base
Main -> sun.security.x509.X500Name JDK internal API (java.base)
JDK Internal API Suggested Replacement
---------------- ---------------------
sun.security.x509.X500Name Use javax.security.auth.x500.X500Principal
@since 1.4
Use jdeps to find problems
24. Using encapsulated APIs
Exception in thread "main" java.lang.IllegalAccessError: class Main
(in unnamed module @0x4cdbe50f) cannot access class
sun.security.x509.X500Name (in module java.base) because module
java.base does not export sun.security.x509 to unnamed module
@0x4cdbe50f
at Main.main(Main.java:4)
Compile with 1.8, run with 9:
25. Using encapsulated APIs
java --add-exports java.base/sun.security.x509=ALL-UNNAMED Main
Exception in thread "main" java.lang.IllegalAccessError: class Main
(in unnamed module @0x4cdbe50f) cannot access class
sun.security.x509.X500Name (in module java.base) because module
java.base does not export sun.security.x509 to unnamed module
@0x4cdbe50f
at Main.main(Main.java:4)
Compile with 1.8, run with 9:
26. Using encapsulated APIs
Exception in thread "main" java.lang.IllegalAccessError: class Main
(in unnamed module @0x4cdbe50f) cannot access class
sun.security.x509.X500Name (in module java.base) because module
java.base does not export sun.security.x509 to unnamed module
@0x4cdbe50f
at Main.main(Main.java:4)
Compile with 1.8, run with 9:
java --permit-illegal-access Main
New 'kill-switch':
38. Automatic Modules
‣ A plain JAR on the module path becomes an
Automatic Module
‣ Module name derived from JAR name
‣ Exports everything
‣ Reads all other modules
45. Open Modules
‣ Reliable configuration like normal modules
‣ Requires for every dependency
‣ Opens all packages for deep reflection
‣ Easy compatibility with existing code and frameworks
‣ Allows reflection
46. Type Compile time Reflection on public Deep reflection
Exports
✓ ✓ ✘
Open
✘ ✓ ✓
Exports + Open
✓ ✓ ✓
47. Open Packages
Opens the package for deep reflection, but not compile
time access
A package can be both open and exported