Successfully reported this slideshow.
Your SlideShare is downloading. ×

Migrating to Java 9 Modules

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

6 of 60 Ad

Migrating to Java 9 Modules

Download to read offline

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 you’ll run into during migration, leading to practical tips and the ability to set realistic goals. It’s 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.

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 you’ll run into during migration, leading to practical tips and the ability to set realistic goals. It’s 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.

Advertisement
Advertisement

More Related Content

Slideshows for you (20)

Similar to Migrating to Java 9 Modules (20)

Advertisement

More from Sander Mak (@Sander_Mak) (20)

Recently uploaded (20)

Advertisement

Migrating to Java 9 Modules

  1. 1. By Sander Mak Migrating to Java 9 Modules @Sander_Mak
  2. 2. Migrating to Java 9 java -cp .. -jar myapp.jarJava 8
  3. 3. Migrating to Java 9 java -cp .. -jar myapp.jar java -cp .. -jar myapp.jar Java 8 Java 9
  4. 4. Today's journey Running on Java 9 Java 9 modules Migrating to modules Modularising code
  5. 5. Migrating to Java 9 Let's try it! java -cp .. -jar myapp.jar java -cp .. -jar myapp.jar Java 8 Java 9
  6. 6. Classpath migration problems ‣ Unresolved platform modules ‣ (Ab)use of platform internals
  7. 7. 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"); } }
  8. 8. Modular JDK
  9. 9. Modular JDK
  10. 10. Modular JDK
  11. 11. Modular JDK
  12. 12. Modular JDK
  13. 13. ‣ 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
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. javac --add-modules java.xml.bind demo/Main.java Add unresolved platform modules java --add-modules java.xml.bind demo/Main.java Let's try it!
  18. 18. 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
  19. 19. 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 = ^
  20. 20. 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
  21. 21. Using encapsulated APIs Compile with 1.8, run with 9:
  22. 22. 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:
  23. 23. 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:
  24. 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: java --permit-illegal-access Main New 'kill-switch':
  25. 25. Back to Modules...
  26. 26. main books.api books.impl bookstore Mental picture of your app
  27. 27. Actual view of your app
  28. 28. main books.api books.impl bookstore Java 9 modules make this possible!
  29. 29. Migrating to modules
  30. 30. Top down migration commons.lang3.3.4.jar demonstrator.jar classpath java.base module path
  31. 31. package com.javamodularity.demonstrator; import org.apache.commons.lang3.StringUtils; public class Demo { public static void main(String args[]) { String output = StringUtils.leftPad("Leftpad FTW!", 20); System.out.println(output); } } Classic classpath
  32. 32. package com.javamodularity.demonstrator; import org.apache.commons.lang3.StringUtils; public class Demo { public static void main(String args[]) { String output = StringUtils.leftPad("Leftpad FTW!", 20); System.out.println(output); } } Classic classpath javac -cp lib/commons-lang3-3.4.jar -d out $(find src -name '*.java') java -cp out:lib/commons-lang3-3.4.jar com.javamodularity.demonstrator.Demo Compile Run
  33. 33. Top down migration commons.lang3.3.4.jar demonstrator.jar classpath java.base module path
  34. 34. Top down migration commons.lang3.3.4.jar demonstrator.jar classpath java.base module path Can’t reference the classpath from named modules!
  35. 35. Top down migration demonstrator.jar classpath java.base module path commons.lang But this isn’t our code!
  36. 36. 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
  37. 37. module demonstrator { requires commons.lang; } Using Automatic Modules
  38. 38. module demonstrator { requires commons.lang; } Using Automatic Modules javac --module-path lib --module-source-path src -d mods $(find src -name '*.java') java --module-path mods:lib -m demonstrator/com.javamodularity.demonstrator.Demo Compile Run
  39. 39. Migrating the books app 1. Create a “monolith module” 2. Declare exports 3. Figure out “requires” 4. Move some libs to Automatic Modules Step 2
  40. 40. commons.pool spring.context classpath Application code module path spring.tx hibernate.core hibernate.jpa javax.inject Automatic modules Named modules commons.logging spring.beans spring.aop spring.core hsqldb spring.orm …
  41. 41. commons.pool spring.context classpath Application code module path spring.tx hibernate.core hibernate.jpa javax.inject Automatic modules Named modules commons.logging spring.beans spring.aop spring.core hsqldb spring.orm …
  42. 42. DEMO
  43. 43. 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
  44. 44. Type Compile time Reflection on public Deep reflection Exports ✓ ✓ ✘ Open ✘ ✓ ✓ Exports + Open ✓ ✓ ✓
  45. 45. Open Packages Opens the package for deep reflection, but not compile time access A package can be both open and exported
  46. 46. Tip 1 Move libraries to module path one by one Start with direct dependencies
  47. 47. Tip 2 open when no compile time dependency should exist (possibly qualified)
  48. 48. Tip 3 requires only for direct dependencies --add-modules for run-time dependencies
  49. 49. Tip 4 Use open modules for framework compatibility Fine tune open later
  50. 50. Migrating the books app 1. Split monolith module 2. Move requires/exports 3. (optional) Move config files Step 3
  51. 51. commons.pool spring.context classpath Application code module path spring.tx hibernate.core hibernate.jpa javax.inject Automatic modules Named modules commons.logging spring.beans spring.aop spring.core hsqldb spring.orm …
  52. 52. commons.pool spring.context classpath module path spring.tx hibernate.core hibernate.jpa javax.inject Automatic modules Named modules commons.logging spring.beans spring.aop spring.core hsqldb spring.orm … main books.api books.impl bookstore
  53. 53. Migration steps - recap
  54. 54. Migration steps - recap ‣ Migrate to Java 9 using classpath only
  55. 55. Migration steps - recap ‣ Migrate to Java 9 using classpath only ‣ Create a module around your whole app
  56. 56. Migration steps - recap ‣ Migrate to Java 9 using classpath only ‣ Create a module around your whole app ‣ Modularize your application!
  57. 57. Thank you. bit.ly/java9book @javamodularity code @ bit.ly/java9mig
  58. 58. Thank you. bit.ly/java9book @javamodularity http://bit.ly/java9course Java 9 Modularity: First Look code @ bit.ly/java9mig

×