Modules   «Prev  Next»

Running Modular Application

You are requiring java.base from your hello.world module and you have built a modular jar for your module.
module hello.world{
exports com.example.hello;
requires java.base;
}

Question: Where do you put a modular jar?
JDK 9 looks for modules in the 1) system image and on the 2) module path.
Running Modular Application
Running Modular Application


System image

The system image is in the JDK installation directory that contains java.base and a few dozen core modules.
The module path is set on the command line like the class path, except that the module path points to directories containing 1) modular jars rather than to the 2) jars themselves.
So java -p mods specifies that modular jars are to be found in the mods directory and

- m hello.world 

specifies the module to run.

Running a Modular Application

The key point, then about modules requiring each other is that the module system can check the requirements and validate that a modular application is sound. Once the compiler or runtime has found the initial module HelloWorld in the system image or on the module path, it performs process called resolution.

Resolution

Resolution means inspecting it requires directives and finding those required modules in the system image or on the module path, then recursively resolving them. We are building a graph whose nodes and modules and whose edges are the requires relation.

Running Modular Application Graph
Running Modular Application Graph


To be clear, in JDK 9 we do this at 1) compile time and 2) run time. If resolution succeeds you get three guarantees: Three
  1. 1) first every module that is required is available, (A big improvement over the class path where you don't discover these identity lists missing jars until later)
  2. 2) second modules do not depend on each other in a cycle. Cyclic dependencies leads to code that is hard to maintain and we are taking the opportunity to prohibit them in modules from day one of JDK 9. You may be thinking this is just dependency management, doesn't Maven do this? Bear in mind that Maven only works at compile time and this resolution process works at runtime as well, but more importantly let me come to resolutions third guarantee
  3. 3) If code in one module imports a package, then the package is exported from exactly one other module. This guarantee relies on a) exports as well as b) "requires" so you won't find it in Maven.
It means there are no split packages.
It means there are no split packages, which is what happens when two jars on the classpath contain the same package and
  1. you load some classes from one jar and
  2. other classes in the same package from the other jar