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
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?
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
“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
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
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
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
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)