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 module-info.java file
• File is placed in the root of the module folder
• Can be packaged as jar
5. module-info.java 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 me.kamkor.foo {
exports me.kamkor.foo.api;
exports me.kamkor.foo;
requires java.sql;
}
Current naming
convention
Same as module
name, confusing
enough?
7. Implied readability
module me.kamkor.foo {
requires java.sql;
}
• Code in the me.kamkor.foo module:
final Driver driver = DriverManager.getDriver(url);
final Logger logger = driver.getParentLogger();
logger.info(“Connection acquired”);
Logger is exported in
the java.logging module
Driver is exported in
the java.sql module
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. 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.”
https://docs.oracle.com/javase/8/docs/technotes/tools/windows/class
path.html
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
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
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. jlink – The Java Linker
$ du -h -d 0 executable
20M executable
$ executable/bin/me.kamkor.greeter
Greetings World!
29. jlink – The Java Linker
$ executable/bin/java -listmods
java.base@9-ea
me.kamkor.greeter
me.kamkor.foo@1.0
Version is just
for information
purposes
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)