JavaOne 2015 saw a series of talks by the Project Jigsaw team about modularity in Java 9. They are all very interesting and full of valuable information and I urge every Java developer to watch them.
Beyond that I want to give the community a way to search and reference them, so I summarize them here:
- Prepare For JDK 9
- Introduction To Modular Development
- Advanced Modular Development
- Under the Hood Of Project Jigsaw
I made an effort to link to as many external resources as possible to keep the individual posts short. The play icons will take you straight to the corresponding point in the ten hour long video streams that Oracle put online for each room and day. (Great format, folks!) Not only did they (so far) fumble the cutting, they also seem to have resorted to low-volume mono sound so make sure to crank up the volume.
After preparing for JDK 9 let's continue with an introduction to modular development!
- Content: Introduction to the module system and the concept of modules
- Speaker: Alan Bateman
- Links: Video and Slides
▚What Is A Module?
Alan Bateman starts by explaining the basic concept of modules as named, self describing collections of code and data. This part is more than covered by The State Of The Module System (SOTMS):
▚Platform Modules
Platform modules are the ones that make up the JDK - SOTMS explains them as well. Their dependency graph is shown on Slide 19:
A very similar graph that includes the OpenJDK-specific modules can be found in JEP 200.
Bateman also mentions java -listmods
, which will list all the available platform modules.
(Note that there are discussions on the mailing list to rename the flag.)
▚Command Line
After explaining the module path Bateman gives an introduction to the various new command line options. The Jigsaw quick-start guide has us covered here.
An option the guide does not mention is java -Xdiag:resolver
, which outputs additional information regarding module dependency resolution.
▚Packaging As Modular JAR
Modules can be packaged into so called modular JARs, which the quick-start guide covers as well.
Bateman stresses that such JARs work both on the module path in Java 9 as well as on the class path in Java 8 (as long as they target 1.8). He also quickly shows how the module path and class path can be mixed to launch a program.
▚Linking
Linking allows to bundle some modules and all of their transitive dependencies into a run-time image. If the initial modules are platform modules, the result will essentially be a variant of the JDK. This is in fact how the current Jigsaw builds are being created.
This is done with the new tool jlink and the quick-start guide shows how to do it.
▚Questions
There were a couple of interesting questions.
▚Is There Any Solution For Optional Dependencies?
In earlier Jigsaw prototypes there was a notion of optional dependencies. Working out the precise semantics turned out to be hard so the feature was not implemented. Research showed that optional dependencies can typically be refactored to services that might or might not be present at runtime.
Services are covered by the quick-start guide.
▚Can jlink Cross-Compile?
Can It Create A Self-Executing File?
"Yes" and "Not directly but other tools will be improved so that will be doable in the future".
▚Can Modules Be Versioned?
Long story short: "Versions are hard, we don't want to replicate build tool functionality, so 'No'". For more, listen to Mark Reinhold's full answer.
▚Can jlink Use Cross-Module Optimizations?
▚How Does JavaDoc Handle Modules?
JavaDoc will be upgraded so that it understands what modules are. It will display them along with packages and classes. And it will also by default not generate documentation for types in not-exported packages.