Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Project Jigsaw - modularity at language level

Learn the basics of the Project Jigsaw and find out what makes it different from tools like Maven.

  • Be the first to comment

  • Be the first to like this

Project Jigsaw - modularity at language level

  1. 1. Project Jigsaw – Modularity at language level Kamil Korzekwa @kamkorz | April 14, 2016
  2. 2. Project Jigsaw • Adds modularity to the Java platform • Hopefully coming to JDK 9 in March 2017 • Will cause compatibility issues for applications that use JDK internal APIs (JEP 260)
  3. 3. How is it different from tools like Maven? Jigsaw is language level mechanism!
  4. 4. What’s a jigsaw module? • A grouping of code • For example Java packages • Can contain other data such as • resources • configuration files • native code (for example when using JNI) • Defined in the file • File is placed in the root of the module folder • Can be packaged as jar
  5. 5. file • Module has a symbolic name (that only looks like a package) • Module exports packages (public API) to other modules • Module requires dependencies to other modules module { exports; exports; requires java.sql; } Current naming convention Same as module name, confusing enough?
  6. 6. public != accessible (fundamental change to Java) • public JDK9 • public to everyone module { exports; } • public, but only to specific modules module { exports to, me.kamkor.magic; } • public only within a module PRE-JDK9
  7. 7. Implied readability module { requires java.sql; } • Code in the module: final Driver driver = DriverManager.getDriver(url); final Logger logger = driver.getParentLogger();“Connection acquired”); Logger is exported in the java.logging module Driver is exported in the java.sql module
  8. 8. Implied readability // assumed configuration (fake) module java.sql { requires java.logging; .. }
  9. 9. Implied readability module java.sql { requires public java.logging; }
  10. 10. Implied readability • If you use types from required module in your exported types, then consider exposing this module to your users using requires public • If you use types from required module only in your internal code, then do not expose this module to your users
  11. 11. “Fun” with classpath bin/greeter/GreetingProvider.class bin/starwarsgreeter/GreetingProvider.class bin/app/GreeterApp.class Using default package for the sake of simplicity
  12. 12. “Fun” with classpath $ java -classpath bin/greeter:bin/app GreeterApp Hello World $ java -classpath bin/starwarsgreeter:bin/app GreeterApp May the force be with you! $ java -classpath bin/greeter:bin/starwarsgreeter:bin/app GreeterApp Hello World
  13. 13. Classpath hell – shadowing • Default classloader loads the first matching class it finds "The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable.” path.html
  14. 14. Classpath hell – real world examples of shadowing • Different libraries on the classpath contain class with the same fully qualified name • Two different versions of the same library are on the classpath • Library is renamed and accidentally added to the classpath twice
  15. 15. Modulepath to the rescue!
  16. 16. Classpath vs modulepath • Flat structure • Allows to locate individual types • Default classloader loads the first matching class it finds on the classpath Modulepath • Graph structure • Allows to locate modules rather than individual types • Java is aware about relationships between the modules • Can discover potential problems sooner Classpath
  17. 17. Shadowing fixed with Jigsaw modulepath • Compiling Java application with two modules on the modulepath that export the same package src/ error: module reads package me.kamkor.beta from both me.kamkor.bar1 and me.kamkor.bar2 Compile error
  18. 18. Shadowing fixed with Jigsaw modulepath • Running Java application with two modules on the modulepath that have the same name Error occurred during initialization of VM java.lang.module.ResolutionException: Two versions of module found in mods Error when trying to run application
  19. 19. Module version selection • Version selection is left for the dependency-resolution mechanisms such as Maven, Gradle, sbt etc. selection
  20. 20. Multiple module versions • Jigsaw allows only one version of module in single configuration • Dependency resolution mechanism must pick one • Modulepath version hell just like classpath version hell /jigsaw/goals-reqs/03#version- selection
  21. 21. Circular dependencies module { requires; } module { requires; } ./src/ error: cyclic dependence involving requires; Compile error
  22. 22. Better JDK with the use of Project Jigsaw
  23. 23. JDK is BIG! • 4000+ classes in rt.jar (classes.jar on Macs) • java.util.List etc. • All loaded on start by the bootstrap classloader • Does your application really need all of them? (source: Liguori, R., Liguori, P.: Java 8 Pocket Guide)
  24. 24. JDK will be split into modules
  25. 25. JDK Structure (simplified) bin java javac jre bin java lib rt.jar lib tools.jar JDK9 bin java javac conf jmods lib Pre-JDK9 jre directory rt.jar tools.jar
  26. 26. jlink – The Java Linker “Create a tool that can assemble and optimize a set of modules and their dependencies into a custom run-time image as defined in JEP 220.”
  27. 27. jlink – The Java Linker $ jlink --modulepath $JAVA_HOME/jmods:mlib --addmods me.kamkor.greeter --output executable --strip-debug --compress=2 $ tree –L 1 executable executable bin conf lib
  28. 28. jlink – The Java Linker $ du -h -d 0 executable 20M executable $ executable/bin/me.kamkor.greeter Greetings World!
  29. 29. jlink – The Java Linker $ executable/bin/java -listmods java.base@9-ea me.kamkor.greeter Version is just for information purposes
  30. 30. Compatibility & Migration • Applications can still use classpath • JDK will be split into modules. Applications may be affected if they used internal APIs of the JDK (JEP 260) • Applications can be split into Jigsaw modules in small steps. Migration guide: • migration
  31. 31. Project Jigsaw summary • Improved maintainability of JDK and user applications • Stronger encapsulation (modules export public APIs) • Reliable configuration with modulepath that replaces error prone classpath mechanism • Improved security of applications • Less APIs make the attack surface smaller • Improved Java platform scalability and flexibility • Configurable modular JDK • Run-time images (jlink – the Java Linker tool) • Improved performance of applications • Start up performance improvements (less classes to load by the bootstrap classloader)
  32. 32. Impact of Project Jigsaw • I hope that it will speed up the development of the JDK • Will it be widely adopted by the developers? • Spring 5 will support Jigsaw out of the box • Spring will do everything it can to allow you to use Jigsaw • Spring jars (spring-mvc, spring-jdbc etc.) will be configured as Jigsaw modules •
  33. 33. Useful resources • • •
  34. 34. Thank you, questions?