Java 9 Modularity and Project Jigsaw

62
JAVA 9 MODULARITY and PROJECT JIGSAW Lightweight Java User Group München 21.02.201 7 Alexandru Jecan Software Architect and Author

Transcript of Java 9 Modularity and Project Jigsaw

JAVA 9 MODUL ARITY and PROJECT J IGSAW

Lightweight Java User Group München21.02.201

7

Alexandru JecanSoftware Architect and Author

SUMMARY1 Need for modularity in Java

02 JAVA 9 MODULARITY and PROJECT JIGSAW

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 5JAVA 9 MODULARITY and PROJECT JIGSAW

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 JIGSAW

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 JIGSAW

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 JIGSAW

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 JIGSAW

SUMMARY

42 Conclusion

43 Resources

44 Documentation

08 JAVA 9 MODULARITY and PROJECT JIGSAW

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 maintainability02

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 JIGSAW

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 JIGSAW

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 JIGSAW

STRONG ENCAPSULATION RELIABLE

CONFIGURATION SECURITY

PERFORMANCE

1 2 3

4 5

PROJECT JIGSAW

SCALABILITY PERFORMANCE

12 JAVA 9 MODULARITY and PROJECT JIGSAW

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 JIGSAW

DEMO 1 List all modules from the Java run-time system with the Java Launcher option --list-modules

14 JAVA 9 MODULARITY and PROJECT JIGSAW

DEMO 2 LIST THE MODULE DESCRIPTOR OF THE MODULE JAVA.NAMING WITH THE OPTION --LIST-MODULES

15 JAVA 9 MODULARITY and PROJECT JIGSAW

JDK MODULE GRAPH IN JAVA )Jdk module graph in java 9

16 JAVA 9 MODULARITY and PROJECT JIGSAW

SOURCE CODE MODULARIZATION The source code reorganized around modules The build system has been changed

17 JAVA 9 MODULARITY and PROJECT JIGSAW

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 JIGSAW

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 JIGSAW

Cla Lorem Ipsum is simply dummy

text of the printing Lorem Ipsum is simply

dummytext of the printing

ENCAPSULATION OF THE INTERNAL APIsThe 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.SignalHandler

sun.misc.Signal sun.reflect.ReflectionFactory

20 JAVA 9 MODULARITY and PROJECT JIGSAW

DEMO 3DEMONSTRATE ENCAPSULATION OF INTERNAL APIS BY ATTEMPTING TO ACCESS A JDK INTERNAL API

21 JAVA 9 MODULARITY and PROJECT JIGSAW

DEMO 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 JIGSAW

DEMO 5DEMONSTRATE HOW TO BREAK THE ENCAPSULATION USING THE --ADD-EXPORTS OPTION

24 JAVA 9 MODULARITY and PROJECT JIGSAW

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 JIGSAW

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 JIGSAW

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 JIGSAW

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 JIGSAW

DEMO 6Show an example using the requires and exports clauses

31 JAVA 9 MODULARITY and PROJECT JIGSAW

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 exported

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 JIGSAW

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 JIGSAW

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 JIGSAW

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 JARsStructure

META-INF/META-INF/MANIFEST.MFmodule-info.classcom/javausergroup/myModule/Main.class….com/javausergroup/myModule/UtilClass.class

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 filejar --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 7Package in order to create a MODULAR JAR

37 JAVA 9 MODULARITY and PROJECT JIGSAW

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 8PRINT THE MODULE DESCRIPTOR OF A MODULAR JAR

39 JAVA 9 MODULARITY and PROJECT JIGSAW

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.classjavac --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 9COMPILE USING THE --MODULE-PATH OPTION

43 JAVA 9 MODULARITY and PROJECT JIGSAW

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 JIGSAW

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

modulejava -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 11RUN THE EXPLODED MODULE

47 JAVA 9 MODULARITY and PROJECT JIGSAW

RUN USING JDK 9Running the modular JAR using the class pathjava -p <list_of_module_directories> -cp <module_name>

<main_class>

48 JAVA 9 MODULARITY and PROJECT JIGSAW

Running the modular JAR using the module pathjava --module-path <list_of_module_directories> -m

<module_name>

DEMO 12RUN THE MODULAR JAR USING THE CLASS PATH

49 JAVA 9 MODULARITY and PROJECT JIGSAW

AND THENRUN 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

50JAVA 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 JIGSAW

MIGRATION – TOP DOWN1) Start with existing modules, run JDeps to find the dependencies2) Put the existing JAR files on the module path in order for them to become automatic modules3) 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 accessibility4) Compile everything: the old modules + the newly generated automatic modules5) Package in order to get modular JARs6) Run using the Java launcher

53 JAVA 9 MODULARITY and PROJECT JIGSAW

MIGRATION – BOTTOM UP1) For every JAR file that you want to migrate, use JDeps to find out which ist dependecies are2) Automatically generate module-info.java files for each JAR using the JDeps tool with option -genmoduleinfo3) 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 need4) Compile all the newly generated modules5) Package all the newly generated modules using the JAR tools in order to get modular JARs6) Attempt to run anything using the Java launcher and use the option --add-mods <module_name> for the modules that need to be resolved54 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

RESOURCESJEP 200 – The Modular JDK http://openjdk.java.net/jeps/200JEP 201 – Modular Source Code http://openjdk.java.net/jeps/201JEP 202 – Modular Run-Time Images http://openjdk.java.net/jeps/220JEP 260 – Encapsulate Most Internal APIs http://openjdk.java.net/jeps/260JEP 261 – Module System http://openjdk.java.net/jeps/261JEP 282 – jlink: The Java Linker http://openjdk.java.net/jeps/282JEP 376 – Java Platform Module System http://openjdk.java.net/projects/jigsaw/specSource code http://hg.openjdk.java.net/jigsaw/jake58 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

[email protected]