JAVA 9 MODULARITY and PROJECT JIGSAW Lightweight Java User Group München 21.02.2017 Alexandru Jecan Software Architect and Author
SUMMARY 1 Need for modularity in Java 02 JAVA 9 MODULARITY and PROJECT 2 Aspects on modularity 3 Project Jigsaw 7 JDK module graph in Java 9 4 JDK modularization 5 Demo 1 6 Demo 2
SUMMARY 9 New structure of JDK and JRE 03 10 Encapsulation of internal APIs 8 Source code modularization 12 Demo 4 11 Demo 3 13 Breaking encapsulation of internal APIs 14 Demo 5 JAVA 9 MODULARITY and PROJECT
SUMMARY 04 19 Implied readability 18 Accessibility changes in Java 9 20 Types of modules 15 New concept of module 16 Module declaration 17 Demo 6 JAVA 9 MODULARITY and PROJECT
SUMMARY 05 18 The Jlink tool 27 The new module path 26 JMOD files 21 Modular JARs 25 Demo 8 24 Packaging to create a modular JAR 22 Packaging to create a modular JAR 23 Demo 7 JAVA 9 MODULARITY and PROJECT
SUMMARY 06 34 Run using JDK 9 29 Demo 9 32 Run using JDK 9 33 Demo 11 28 Compilation using JDK 9 30 Compilation of multiple modules 31 Demo 10 JAVA 9 MODULARITY and PROJECT
SUMMARY 07 35 Demo 12 36 The Jlink tool 37 The enhanced Jdeps tool 38 Demo 13 39 Migration – top - down 40 Migration – bottom - up 41 How to prepare for Jigsaw JAVA 9 MODULARITY and PROJECT
SUMMARY 42 Conclusion 43 Resources 44 Documentation 08 JAVA 9 MODULARITY and PROJECT
NEED FOR MODULARITY IN JAVA 01 The big indivisible and monolithic JDK 02 The class path  Hard to install it on small devices Low level of maintainability 02  JVM breaks the execution at run-time when a JAR is missing Conflicts between versions of JARs  Slow loading of classes from the class path  Dependencies between JARs are not fulfilled 09 JAVA 9 MODULARITY and PROJECT
MAINTAINABILITY High module cohesion REUSABILITY But also leap into electron typesetting, remaining essent. Lorem Ipsum is simply dummy. But also leap into electron typesetting, remaining essent. Email Marketing 1 2 4 3 Low module coupling ASPECTS ON MODULARITY 10 JAVA 9 MODULARITY and PROJECT
But also leap into electron typesetting, remaining essent. Lorem Ipsum is simply dummy. But also leap into electron typesetting, remaining essent. Email Marketing PROJECT JIGSAW What is Project Jigsaw?1 Goals of Project Jigsaw2 11 JAVA 9 MODULARITY and PROJECT
STRONG ENCAPSULATION RELIABLE CONFIGURATION SECURITY PERFORMANCE 1 2 3 4 5 PROJECT JIGSAW SCALABILITY PERFORMANCE 12 JAVA 9 MODULARITY and PROJECT
JDK MODULARIZATION  JDK divided into a set of modules  Platform modules  Portability ensured as source code that depends only upon Java SE modules will depend only upon standard SE types Standard modules (standard API packages, non-standard API packages, can depend upon non-standard modules) Non-standard modules (do no export standard API packages) 13 JAVA 9 MODULARITY and PROJECT
DEMO 1 List all modules from the Java run- time system with the Java Launcher option --list-modules 14 JAVA 9 MODULARITY and PROJECT
DEMO 2 LIST THE MODULE DESCRIPTOR OF THE MODULE JAVA.NAMING WITH THE OPTION --LIST-MODULES 15 JAVA 9 MODULARITY and PROJECT
JDK MODULE GRAPH IN JAVA ) Jdk module graph in java 9 16 JAVA 9 MODULARITY and PROJECT
SOURCE CODE MODULARIZATION  The source code reorganized around modules The build system has been changed 17 JAVA 9 MODULARITY and PROJECT
NEW STRUCTURE OF THE JDK AND JRE  Binary structure of the JDK and JRE changed  Tools.jar and rt.jar removed 18 JAVA 9 MODULARITY and PROJECT
 Most of the internal JDK APIs are inaccessible in Java 9  Trying to access them causes a compilation error ENCAPSULATION OF THE INTERNAL APIs  Internal APIs are in the sun.* package, but not only 19 JAVA 9 MODULARITY and PROJECT
Cla Lorem Ipsum is simply dummy text of the printing Lorem Ipsum is simply dummy text of the printing ENCAPSULATION OF THE INTERNAL APIs The module jdk.unsupported is still accessible // module-info.java (module jdk.unsupported) module jdk.unsupported { exports sun.misc; exports sun.reflect; exports com.sun.nio.file; } sun.misc.Unsafe sun.reflect.Reflection sun.misc.SignalHandle r sun.misc.Signal sun.reflect.ReflectionFactory 20 JAVA 9 MODULARITY and PROJECT
DEMO 3 DEMONSTRATE ENCAPSULATION OF INTERNAL APIS BY ATTEMPTING TO ACCESS A JDK INTERNAL API 21 JAVA 9 MODULARITY and PROJECT
DEM O 3 22 INTRODUCTION TO JAVA 9 MODULARITY
 The option --add-exports helps us break the encapsulation BREAKING ENCAPSULATION OF THE INTERNAL APIs 23 JAVA 9 MODULARITY and PROJECT
DEMO 5 DEMONSTRATE HOW TO BREAK THE ENCAPSULATION USING THE --ADD-EXPORTS OPTION 24 JAVA 9 MODULARITY and PROJECT
SOURCE FILES GROUPED AS PACKAGES MODULE-INFO.JAVA FILE RESOURCE FILES NATIVE SOURCE CODE CONFIGURATION FILESNEW CONCEPT OF MODULE 25 JAVA 9 MODULARITY and PROJECT
REQUIRES EXPORTS PROVIDES USES OPENS MODULE DECLARATION  New file called module-info.java located in the top level directory of the sources directory module com.javausergroup.myModule { ……………. } 5 types of clauses: 26 JAVA 9 MODULARITY and PROJECT
MODULE DECLARATION module com.javausergroup.myFirstModule { requires com.javausergroup.mySecondModule; } The requires clause  Specifies a dependency on another module  Defines readability between modules 27 JAVA 9 MODULARITY and PROJECT
MODULE DECLARATION module com.javausergroup.mySecondModule { exports com.javausergroup.myFirstPackage; exports com.javausergroup.mySecondPackage to myThirdModule, to mySecondModule; } The exports clause  Specifies which packages are exported to other modules The second export is a qualified export 28 JAVA 9 MODULARITY and PROJECT
DEMO 6 Show an example using the requires and exports clauses 31 JAVA 9 MODULARITY and PROJECT
ACCESSIBILTIY CHANGES IN JAVA 9  3 conditions have to be simultaneously met in order to get access in Java 9  Core reflection does not work Packages have to be exporte The type has to be public The module has to read the other module  Simply setting a “public” modifier to a type does not mean that access is applied 30 JAVA 9 MODULARITY and PROJECT
IMPLIED READABILITY  Everyone that depends on module java.sql will now also depend on java.logging  Implied readability is achieved with „requires transitive <module_name>“ package java.sql; import java.util.logging.Logger; public interface Driver { public Logger getParentLogger(); } module java.sql { requires transitive java.logging; … }  Previous „transitive“ was called „public“ 31 JAVA 9 MODULARITY and PROJECT JIGSAW
TYPES OF MODULES 32 JAVA 9 MODULARITY and PROJECT Strong modulesmodule com.javausergroup.myStrongModule { requires java.sql; exports myFirstPackage; exports private mySecondPackage; }  Does not export any of its packages by default  Exports clauses must be explicitly specified  Exports clauses export packages at compile time as well as at run- time Exports clauses has to use „exports private“ in order for their public types to be suitable for reflection
TYPES OF MODULES 33 JAVA 9 MODULARITY and PROJECT Automatic modules Module resulted after placing a JAR file on the module path  Requires all the existing modules (all own modules + all modules from the JDK image + all other automatic modules) Exports all of its packages  Can access types on the class path  Useful for 3rd party code  Does not have to be explicitly declared Observable modules Represent all the modules from the system  Platform modules + library modules + our own modules  The modules from the module path are part of the observable modules
MODULAR JARs  JAR files that additionally contain a module-info.class file  Can be used on the module path as well as on the class path  Backward compatible  Can comprise only one module  Useful to replace a group of compiled files  Built with the JAR tool  When placed on the class path, the modular JAR acts like a normal JAR file (module-info.class file is ignored) 34 JAVA 9 MODULARITY and PROJECT JIGSAW
MODULAR JARs Structure META-INF/ META-INF/MANIFEST.MF module-info.class com/javausergroup/myModule/Main.class …. com/javausergroup/myModule/UtilClass.clas s 35 JAVA 9 MODULARITY and PROJECT JIGSAW
PACKAGING TO CREATE A MODULAR JAR  Package the content of a module in order to create a modular JAR file jar --create --file <directory_name>/<jar_file.jar> -- main-class <class_name> -C <output_directory>  <jar_file.jar> specifies the name of the modular JAR that is being created  --main-class <class_name> sets the main class of the module  -C<output_directory> specifies that all the compiled files from <output_directory> should be put in the JAR file 36 JAVA 9 MODULARITY and PROJECT JIGSAW
DEMO 7 Package in order to create a MODULAR JAR 37 JAVA 9 MODULARITY and PROJECT
PACKAGING TO CREATE A MODULAR JAR jar --file <jar_file.jar> -p  -p prints the module descriptor: the name of the module, the modules that it requires, the name of the main class, etc.  A modular JAR file contains class files: for source class files and for the module-info.class file 38 JAVA 9 MODULARITY and PROJECT JIGSAW
DEMO 8 PRINT THE MODULE DESCRIPTOR OF A MODULAR JAR 39 JAVA 9 MODULARITY and PROJECT
JMOD FILES  Similar to modular JARs, but they can also contain native code  Have the extension „*.jmod“  Should be used when modular JAR files cannot be applied (for instance because some JDK modules have native code, etc)  Can be found in the jmods directory of JDK 9; each module corresponds to a JMOD file  Used to build the JDK images 40 JAVA 9 MODULARITY and PROJECT JIGSAW
THE NEW MODULE PATH  The module path represents a sequence of directories that contain modules (which can be expanded as class files or packaged)  It is used by the compiler to find the modules in order to resolve them  Specified using the --module-path option, followed by the sequence of directories  The Java compiler used the new option --module-path <path_to_directories> or –p <path_to_directories>  Two versions of a module in the same directory are not allowed  The module path can be mixed together with the class path (in this case the classes that are part of the modules are able to depend on anything that exists on the class path) 41 JAVA 9 MODULARITY and PROJECT JIGSAW
COMPILATION USING JDK 9  Module resolution is invoked – compute a minimal required set of modules given a dependency graph and a root module chosen from the graph  module-info.java file also get compiled => results in module-info.class javac --module-path <list_of_directories> –d <output_directory> <path_to_the_module_info.java_file> <path_to_other_java_files>  -d option specifies the output directory where the compiled files will be located  --module-path specifies the directory / directories where to find all the modules that are already compiled 42 JAVA 9 MODULARITY and PROJECT JIGSAW
DEMO 9 COMPILE USING THE --MODULE-PATH OPTION 43 JAVA 9 MODULARITY and PROJECT
COMPILATION OF MULTIPLE MODULES  Necessary when the module to be compiled has dependencies on another modules javac –d <output_directory> --module-source-path src $(find . –name “*.java”)  --module-source-path is another option that can be used if the modules are not yet compiled. It specifies the module definitions 44 JAVA 9 MODULARITY and PROJECT JIGSAW
DEMO 10 COMPILE USING THE --MODULE-SOURCE-PATH OPTION 45 JAVA 9 MODULARITY and PROJECT
RUN USING JDK 9  By trying to run the application, the module is loaded and its dependencies are solved; then the Main class is executed Running the exploded module java -p <list_of_module_directories> -m <module_name>/<main_class_name>  -p or --module-path represent the module path  -m or --module represent the module => starts the resolution process by finding the dependencies and building the module graph  -Xdiag:resolver can be used in order to see what is going on during the resolution process 46 JAVA 9 MODULARITY and PROJECT JIGSAW
DEMO 11 RUN THE EXPLODED MODULE 47 JAVA 9 MODULARITY and PROJECT
RUN USING JDK 9 Running the modular JAR using the class path java -p <list_of_module_directories> -cp <module_name> <main_class> 48 JAVA 9 MODULARITY and PROJECT JIGSAW Running the modular JAR using the module path java --module-path <list_of_module_directories> -m <module_name>
DEMO 12 RUN THE MODULAR JAR USING THE CLASS PATH 49 JAVA 9 MODULARITY and PROJECT AND THEN RUN THE MODULAR JAR USING THE MODULE PATH
THE JLINK TOOL jlink --module-path <list_of_directories> --add-modules <module_name> --output <directory_for_output>  Tool used to assemble a set of modules together with their dependencies into a custom run-time image  --module-path: the list of directories where the modules will be found (platform modules also have to be added !)  --add-modules: specifies the name of the module that will be added to the run-time image; the transitive dependencies of it will be searched and they will be added into the run-time image too  --output: specifies the location where the run-time image will be created 50 JAVA 9 MODULARITY and PROJECT JIGSAW
THE ENHANCED JDEPS TOOL  Used to perform statical analysis on a library  The option --jdkinternals used to find dependencies on any unsupported JDK internal APIs 51 JAVA 9 MODULARITY and PROJECT JIGSAW
DEMO 13 RUN JDEPS ON GUAVA WITH OPTION –JDKINTERNALS IN ORDER TO FIND ITS DEPENDENCIES ON ANY UNSUPPORTED JDK INTERNAL APIS 52 JAVA 9 MODULARITY and PROJECT
MIGRATION – TOP DOWN 1) Start with existing modules, run JDeps to find the dependencies 2) Put the existing JAR files on the module path in order for them to become automatic modules 3) Edit the module-info.java files of the modules that are above the JAR files in the module graph and add requires clauses to the newly generated automatic modules in order to get accessibility 4) Compile everything: the old modules + the newly generated automatic modules 5) Package in order to get modular JARs 6) Run using the Java launcher 53 JAVA 9 MODULARITY and PROJECT JIGSAW
MIGRATION – BOTTOM UP 1) For every JAR file that you want to migrate, use JDeps to find out which ist dependecies are 2) Automatically generate module-info.java files for each JAR using the JDeps tool with option -genmoduleinfo 3) As for each package of the previous JAR file a generate export will be generated, manually delete the exports statements from the module- info.java file that you don‘t need 4) Compile all the newly generated modules 5) Package all the newly generated modules using the JAR tools in order to get modular JARs 6) Attempt to run anything using the Java launcher and use the option -- add-mods <module_name> for the modules that need to be resolved 54 JAVA 9 MODULARITY and PROJECT JIGSAW
HOW TO PREPARE FOR JIGSAW  Avoid JDK internal APIs in your code. Find a solution to replace them  Avoid having dependencies of „rt.jar“ or „tools.jar“ in your code  Avoid invoke throughout your code one of the 6 methods that were removed (LogManager.addPropertyChangeListener, Packer.addPropertyChangeListener, Unpacker.addPropertyChangeListener, etc.)  Avoid using the old version string format in your code  Check for split packages 55 JAVA 9 MODULARITY and PROJECT JIGSAW
HOW TO PREPARE FOR JIGSAW  There are 6 modules that are shared with Java Enteprise Edition and as a result are not resolved by default: java.xml.bind, java.xml.ws, java.activation, java.annotations.common, java.transaction, java.corba  In order to avoid a NoClassDefFoundError, make the modules above resolvable by using the option --add-modules <module_name> or by deploying them in the class path using --class-path <jar_file_name.jar> 56 JAVA 9 MODULARITY and PROJECT JIGSAW
CONCLUSION  Project Jigsaw is one of the biggest changes in the Java Platform since it was introduced back in 1995  Project Jigsaw is a very complex subject  Project Jigsaw addresses a couple of important existing problems of the Java Platform  Project Jigsaw changes the way we architect and design software applications using Java 9 57 JAVA 9 MODULARITY and PROJECT JIGSAW
RESOURCES JEP 200 – The Modular JDK http://openjdk.java.net/jeps/200 JEP 201 – Modular Source Code http://openjdk.java.net/jeps/201 JEP 202 – Modular Run-Time Images http://openjdk.java.net/jeps/220 JEP 260 – Encapsulate Most Internal APIs http://openjdk.java.net/jeps/260 JEP 261 – Module System http://openjdk.java.net/jeps/261 JEP 282 – jlink: The Java Linker http://openjdk.java.net/jeps/282 JEP 376 – Java Platform Module System http://openjdk.java.net/projects/jigsaw/spec Source code http://hg.openjdk.java.net/jigsaw/jake 58 JAVA 9 MODULARITY and PROJECT JIGSAW
DOCUMENTATION Jigsaw official OpenJDK website: http://openjdk.java.net/projects/jigsaw Specification document: http://openjdk.java.net/projects/jigsaw/spec/reqs Jigsaw Development mailing list: http://mail.openjdk.java.net/pipermail/jigsaw-dev Jigsaw Expert Group mailing list: http://mail.openjdk.java.net/pipermail/jpms-spec-experts 59 JAVA 9 MODULARITY and PROJECT JIGSAW
DOCUMENTATION Jigsaw Adoption Discuss mailing list: http://mail.openjdk.java.net/pipermail/adoption-discuss API Specification for the Java 9 Standard Edition : http://cr.openjdk.java.net/~mr/jigsaw/spec/api Specification „State of the Module System : http://openjdk.java.net/projects/jigsaw/spec/otms Issue summary: http://openjdk.java.net/projects/jigsaw/spec/issues 60 JAVA 9 MODULARITY and PROJECT JIGSAW
DOWNLOAD Download JDK 9 early access builds with Project Jigsaw : https://jdk9.java.net/jigsaw 61 JAVA 9 MODULARITY and PROJECT JIGSAW
Thank you for your contribution at the meeting facebook/alexandru.jecan @alexandrujecan alexandrujecan alexjecan88@yahoo.com

Java 9 Modularity and Project Jigsaw

  • 1.
    JAVA 9 MODULARITYand PROJECT JIGSAW Lightweight Java User Group München 21.02.2017 Alexandru Jecan Software Architect and Author
  • 2.
    SUMMARY 1 Need formodularity in Java 02 JAVA 9 MODULARITY and PROJECT 2 Aspects on modularity 3 Project Jigsaw 7 JDK module graph in Java 9 4 JDK modularization 5 Demo 1 6 Demo 2
  • 3.
    SUMMARY 9 New structureof JDK and JRE 03 10 Encapsulation of internal APIs 8 Source code modularization 12 Demo 4 11 Demo 3 13 Breaking encapsulation of internal APIs 14 Demo 5 JAVA 9 MODULARITY and PROJECT
  • 4.
    SUMMARY 04 19 Implied readability 18Accessibility changes in Java 9 20 Types of modules 15 New concept of module 16 Module declaration 17 Demo 6 JAVA 9 MODULARITY and PROJECT
  • 5.
    SUMMARY 05 18 The Jlinktool 27 The new module path 26 JMOD files 21 Modular JARs 25 Demo 8 24 Packaging to create a modular JAR 22 Packaging to create a modular JAR 23 Demo 7 JAVA 9 MODULARITY and PROJECT
  • 6.
    SUMMARY 06 34 Run usingJDK 9 29 Demo 9 32 Run using JDK 9 33 Demo 11 28 Compilation using JDK 9 30 Compilation of multiple modules 31 Demo 10 JAVA 9 MODULARITY and PROJECT
  • 7.
    SUMMARY 07 35 Demo 12 36The Jlink tool 37 The enhanced Jdeps tool 38 Demo 13 39 Migration – top - down 40 Migration – bottom - up 41 How to prepare for Jigsaw JAVA 9 MODULARITY and PROJECT
  • 8.
    SUMMARY 42 Conclusion 43 Resources 44Documentation 08 JAVA 9 MODULARITY and PROJECT
  • 9.
    NEED FOR MODULARITYIN JAVA 01 The big indivisible and monolithic JDK 02 The class path  Hard to install it on small devices Low level of maintainability 02  JVM breaks the execution at run-time when a JAR is missing Conflicts between versions of JARs  Slow loading of classes from the class path  Dependencies between JARs are not fulfilled 09 JAVA 9 MODULARITY and PROJECT
  • 10.
    MAINTAINABILITY High module cohesion REUSABILITY Butalso leap into electron typesetting, remaining essent. Lorem Ipsum is simply dummy. But also leap into electron typesetting, remaining essent. Email Marketing 1 2 4 3 Low module coupling ASPECTS ON MODULARITY 10 JAVA 9 MODULARITY and PROJECT
  • 11.
    But also leapinto electron typesetting, remaining essent. Lorem Ipsum is simply dummy. But also leap into electron typesetting, remaining essent. Email Marketing PROJECT JIGSAW What is Project Jigsaw?1 Goals of Project Jigsaw2 11 JAVA 9 MODULARITY and PROJECT
  • 12.
    STRONG ENCAPSULATION RELIABLE CONFIGURATION SECURITY PERFORMANCE 1 2 3 45 PROJECT JIGSAW SCALABILITY PERFORMANCE 12 JAVA 9 MODULARITY and PROJECT
  • 13.
    JDK MODULARIZATION  JDKdivided into a set of modules  Platform modules  Portability ensured as source code that depends only upon Java SE modules will depend only upon standard SE types Standard modules (standard API packages, non-standard API packages, can depend upon non-standard modules) Non-standard modules (do no export standard API packages) 13 JAVA 9 MODULARITY and PROJECT
  • 14.
    DEMO 1 List allmodules from the Java run- time system with the Java Launcher option --list-modules 14 JAVA 9 MODULARITY and PROJECT
  • 15.
    DEMO 2 LIST THEMODULE DESCRIPTOR OF THE MODULE JAVA.NAMING WITH THE OPTION --LIST-MODULES 15 JAVA 9 MODULARITY and PROJECT
  • 16.
    JDK MODULE GRAPH INJAVA ) Jdk module graph in java 9 16 JAVA 9 MODULARITY and PROJECT
  • 17.
    SOURCE CODE MODULARIZATION The source code reorganized around modules The build system has been changed 17 JAVA 9 MODULARITY and PROJECT
  • 18.
    NEW STRUCTURE OFTHE JDK AND JRE  Binary structure of the JDK and JRE changed  Tools.jar and rt.jar removed 18 JAVA 9 MODULARITY and PROJECT
  • 19.
     Most ofthe internal JDK APIs are inaccessible in Java 9  Trying to access them causes a compilation error ENCAPSULATION OF THE INTERNAL APIs  Internal APIs are in the sun.* package, but not only 19 JAVA 9 MODULARITY and PROJECT
  • 20.
    Cla Lorem Ipsumis simply dummy text of the printing Lorem Ipsum is simply dummy text of the printing ENCAPSULATION OF THE INTERNAL APIs The module jdk.unsupported is still accessible // module-info.java (module jdk.unsupported) module jdk.unsupported { exports sun.misc; exports sun.reflect; exports com.sun.nio.file; } sun.misc.Unsafe sun.reflect.Reflection sun.misc.SignalHandle r sun.misc.Signal sun.reflect.ReflectionFactory 20 JAVA 9 MODULARITY and PROJECT
  • 21.
    DEMO 3 DEMONSTRATE ENCAPSULATION OFINTERNAL APIS BY ATTEMPTING TO ACCESS A JDK INTERNAL API 21 JAVA 9 MODULARITY and PROJECT
  • 22.
    DEM O 3 22 INTRODUCTION TOJAVA 9 MODULARITY
  • 23.
     The option--add-exports helps us break the encapsulation BREAKING ENCAPSULATION OF THE INTERNAL APIs 23 JAVA 9 MODULARITY and PROJECT
  • 24.
    DEMO 5 DEMONSTRATE HOWTO BREAK THE ENCAPSULATION USING THE --ADD-EXPORTS OPTION 24 JAVA 9 MODULARITY and PROJECT
  • 25.
  • 26.
    REQUIRES EXPORTS PROVIDESUSES OPENS MODULE DECLARATION  New file called module-info.java located in the top level directory of the sources directory module com.javausergroup.myModule { ……………. } 5 types of clauses: 26 JAVA 9 MODULARITY and PROJECT
  • 27.
    MODULE DECLARATION module com.javausergroup.myFirstModule{ requires com.javausergroup.mySecondModule; } The requires clause  Specifies a dependency on another module  Defines readability between modules 27 JAVA 9 MODULARITY and PROJECT
  • 28.
    MODULE DECLARATION module com.javausergroup.mySecondModule{ exports com.javausergroup.myFirstPackage; exports com.javausergroup.mySecondPackage to myThirdModule, to mySecondModule; } The exports clause  Specifies which packages are exported to other modules The second export is a qualified export 28 JAVA 9 MODULARITY and PROJECT
  • 29.
    DEMO 6 Showan example using the requires and exports clauses 31 JAVA 9 MODULARITY and PROJECT
  • 30.
    ACCESSIBILTIY CHANGES INJAVA 9  3 conditions have to be simultaneously met in order to get access in Java 9  Core reflection does not work Packages have to be exporte The type has to be public The module has to read the other module  Simply setting a “public” modifier to a type does not mean that access is applied 30 JAVA 9 MODULARITY and PROJECT
  • 31.
    IMPLIED READABILITY  Everyonethat depends on module java.sql will now also depend on java.logging  Implied readability is achieved with „requires transitive <module_name>“ package java.sql; import java.util.logging.Logger; public interface Driver { public Logger getParentLogger(); } module java.sql { requires transitive java.logging; … }  Previous „transitive“ was called „public“ 31 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 32.
    TYPES OF MODULES 32 JAVA9 MODULARITY and PROJECT Strong modulesmodule com.javausergroup.myStrongModule { requires java.sql; exports myFirstPackage; exports private mySecondPackage; }  Does not export any of its packages by default  Exports clauses must be explicitly specified  Exports clauses export packages at compile time as well as at run- time Exports clauses has to use „exports private“ in order for their public types to be suitable for reflection
  • 33.
    TYPES OF MODULES 33 JAVA9 MODULARITY and PROJECT Automatic modules Module resulted after placing a JAR file on the module path  Requires all the existing modules (all own modules + all modules from the JDK image + all other automatic modules) Exports all of its packages  Can access types on the class path  Useful for 3rd party code  Does not have to be explicitly declared Observable modules Represent all the modules from the system  Platform modules + library modules + our own modules  The modules from the module path are part of the observable modules
  • 34.
    MODULAR JARs  JARfiles that additionally contain a module-info.class file  Can be used on the module path as well as on the class path  Backward compatible  Can comprise only one module  Useful to replace a group of compiled files  Built with the JAR tool  When placed on the class path, the modular JAR acts like a normal JAR file (module-info.class file is ignored) 34 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 35.
  • 36.
    PACKAGING TO CREATEA MODULAR JAR  Package the content of a module in order to create a modular JAR file jar --create --file <directory_name>/<jar_file.jar> -- main-class <class_name> -C <output_directory>  <jar_file.jar> specifies the name of the modular JAR that is being created  --main-class <class_name> sets the main class of the module  -C<output_directory> specifies that all the compiled files from <output_directory> should be put in the JAR file 36 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 37.
    DEMO 7 Packagein order to create a MODULAR JAR 37 JAVA 9 MODULARITY and PROJECT
  • 38.
    PACKAGING TO CREATEA MODULAR JAR jar --file <jar_file.jar> -p  -p prints the module descriptor: the name of the module, the modules that it requires, the name of the main class, etc.  A modular JAR file contains class files: for source class files and for the module-info.class file 38 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 39.
    DEMO 8 PRINTTHE MODULE DESCRIPTOR OF A MODULAR JAR 39 JAVA 9 MODULARITY and PROJECT
  • 40.
    JMOD FILES  Similarto modular JARs, but they can also contain native code  Have the extension „*.jmod“  Should be used when modular JAR files cannot be applied (for instance because some JDK modules have native code, etc)  Can be found in the jmods directory of JDK 9; each module corresponds to a JMOD file  Used to build the JDK images 40 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 41.
    THE NEW MODULEPATH  The module path represents a sequence of directories that contain modules (which can be expanded as class files or packaged)  It is used by the compiler to find the modules in order to resolve them  Specified using the --module-path option, followed by the sequence of directories  The Java compiler used the new option --module-path <path_to_directories> or –p <path_to_directories>  Two versions of a module in the same directory are not allowed  The module path can be mixed together with the class path (in this case the classes that are part of the modules are able to depend on anything that exists on the class path) 41 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 42.
    COMPILATION USING JDK9  Module resolution is invoked – compute a minimal required set of modules given a dependency graph and a root module chosen from the graph  module-info.java file also get compiled => results in module-info.class javac --module-path <list_of_directories> –d <output_directory> <path_to_the_module_info.java_file> <path_to_other_java_files>  -d option specifies the output directory where the compiled files will be located  --module-path specifies the directory / directories where to find all the modules that are already compiled 42 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 43.
    DEMO 9 COMPILEUSING THE --MODULE-PATH OPTION 43 JAVA 9 MODULARITY and PROJECT
  • 44.
    COMPILATION OF MULTIPLEMODULES  Necessary when the module to be compiled has dependencies on another modules javac –d <output_directory> --module-source-path src $(find . –name “*.java”)  --module-source-path is another option that can be used if the modules are not yet compiled. It specifies the module definitions 44 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 45.
    DEMO 10 COMPILE USINGTHE --MODULE-SOURCE-PATH OPTION 45 JAVA 9 MODULARITY and PROJECT
  • 46.
    RUN USING JDK9  By trying to run the application, the module is loaded and its dependencies are solved; then the Main class is executed Running the exploded module java -p <list_of_module_directories> -m <module_name>/<main_class_name>  -p or --module-path represent the module path  -m or --module represent the module => starts the resolution process by finding the dependencies and building the module graph  -Xdiag:resolver can be used in order to see what is going on during the resolution process 46 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 47.
    DEMO 11 RUNTHE EXPLODED MODULE 47 JAVA 9 MODULARITY and PROJECT
  • 48.
    RUN USING JDK9 Running the modular JAR using the class path java -p <list_of_module_directories> -cp <module_name> <main_class> 48 JAVA 9 MODULARITY and PROJECT JIGSAW Running the modular JAR using the module path java --module-path <list_of_module_directories> -m <module_name>
  • 49.
    DEMO 12 RUN THEMODULAR JAR USING THE CLASS PATH 49 JAVA 9 MODULARITY and PROJECT AND THEN RUN THE MODULAR JAR USING THE MODULE PATH
  • 50.
    THE JLINK TOOL jlink--module-path <list_of_directories> --add-modules <module_name> --output <directory_for_output>  Tool used to assemble a set of modules together with their dependencies into a custom run-time image  --module-path: the list of directories where the modules will be found (platform modules also have to be added !)  --add-modules: specifies the name of the module that will be added to the run-time image; the transitive dependencies of it will be searched and they will be added into the run-time image too  --output: specifies the location where the run-time image will be created 50 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 51.
    THE ENHANCED JDEPSTOOL  Used to perform statical analysis on a library  The option --jdkinternals used to find dependencies on any unsupported JDK internal APIs 51 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 52.
    DEMO 13 RUN JDEPSON GUAVA WITH OPTION –JDKINTERNALS IN ORDER TO FIND ITS DEPENDENCIES ON ANY UNSUPPORTED JDK INTERNAL APIS 52 JAVA 9 MODULARITY and PROJECT
  • 53.
    MIGRATION – TOPDOWN 1) Start with existing modules, run JDeps to find the dependencies 2) Put the existing JAR files on the module path in order for them to become automatic modules 3) Edit the module-info.java files of the modules that are above the JAR files in the module graph and add requires clauses to the newly generated automatic modules in order to get accessibility 4) Compile everything: the old modules + the newly generated automatic modules 5) Package in order to get modular JARs 6) Run using the Java launcher 53 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 54.
    MIGRATION – BOTTOMUP 1) For every JAR file that you want to migrate, use JDeps to find out which ist dependecies are 2) Automatically generate module-info.java files for each JAR using the JDeps tool with option -genmoduleinfo 3) As for each package of the previous JAR file a generate export will be generated, manually delete the exports statements from the module- info.java file that you don‘t need 4) Compile all the newly generated modules 5) Package all the newly generated modules using the JAR tools in order to get modular JARs 6) Attempt to run anything using the Java launcher and use the option -- add-mods <module_name> for the modules that need to be resolved 54 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 55.
    HOW TO PREPAREFOR JIGSAW  Avoid JDK internal APIs in your code. Find a solution to replace them  Avoid having dependencies of „rt.jar“ or „tools.jar“ in your code  Avoid invoke throughout your code one of the 6 methods that were removed (LogManager.addPropertyChangeListener, Packer.addPropertyChangeListener, Unpacker.addPropertyChangeListener, etc.)  Avoid using the old version string format in your code  Check for split packages 55 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 56.
    HOW TO PREPAREFOR JIGSAW  There are 6 modules that are shared with Java Enteprise Edition and as a result are not resolved by default: java.xml.bind, java.xml.ws, java.activation, java.annotations.common, java.transaction, java.corba  In order to avoid a NoClassDefFoundError, make the modules above resolvable by using the option --add-modules <module_name> or by deploying them in the class path using --class-path <jar_file_name.jar> 56 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 57.
    CONCLUSION  Project Jigsawis one of the biggest changes in the Java Platform since it was introduced back in 1995  Project Jigsaw is a very complex subject  Project Jigsaw addresses a couple of important existing problems of the Java Platform  Project Jigsaw changes the way we architect and design software applications using Java 9 57 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 58.
    RESOURCES JEP 200 –The Modular JDK http://openjdk.java.net/jeps/200 JEP 201 – Modular Source Code http://openjdk.java.net/jeps/201 JEP 202 – Modular Run-Time Images http://openjdk.java.net/jeps/220 JEP 260 – Encapsulate Most Internal APIs http://openjdk.java.net/jeps/260 JEP 261 – Module System http://openjdk.java.net/jeps/261 JEP 282 – jlink: The Java Linker http://openjdk.java.net/jeps/282 JEP 376 – Java Platform Module System http://openjdk.java.net/projects/jigsaw/spec Source code http://hg.openjdk.java.net/jigsaw/jake 58 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 59.
    DOCUMENTATION Jigsaw official OpenJDKwebsite: http://openjdk.java.net/projects/jigsaw Specification document: http://openjdk.java.net/projects/jigsaw/spec/reqs Jigsaw Development mailing list: http://mail.openjdk.java.net/pipermail/jigsaw-dev Jigsaw Expert Group mailing list: http://mail.openjdk.java.net/pipermail/jpms-spec-experts 59 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 60.
    DOCUMENTATION Jigsaw Adoption Discussmailing list: http://mail.openjdk.java.net/pipermail/adoption-discuss API Specification for the Java 9 Standard Edition : http://cr.openjdk.java.net/~mr/jigsaw/spec/api Specification „State of the Module System : http://openjdk.java.net/projects/jigsaw/spec/otms Issue summary: http://openjdk.java.net/projects/jigsaw/spec/issues 60 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 61.
    DOWNLOAD Download JDK 9early access builds with Project Jigsaw : https://jdk9.java.net/jigsaw 61 JAVA 9 MODULARITY and PROJECT JIGSAW
  • 62.
    Thank you for your contribution atthe meeting facebook/alexandru.jecan @alexandrujecan alexandrujecan alexjecan88@yahoo.com