Examining module resolution in action

There's a new command option added to Java that prints out debug information that describes the module resolution process. You can activate it by passing the option --show-module-resolution. When passed this option, the java command prints out console messages for each step of the module resolution. You can use this to see the process that the runtime goes through to resolve all the modules, much like we did in the preceding exercise.

This is how you ran the command-line address book module in the previous chapter, without the flag:

$ java --module-path out -m packt.addressbook/packt.addressbook.Main

Here's how you run it with the module resolution diagnostics enabled:

$ java --show-module-resolution --module-path out -m 
packt.addressbook/packt.addressbook.Main

The verbose output gives a clear indication of what's happening here. Things start off with the root module packt.addressbook, as expected:

root packt.addressbook file:///Users/koushik/code/java9/07-
services/out/packt.addressbook/

Next, it finds the dependent modules from the module descriptor. For every module it finds, the output lists the name of the module, where it found it (the path), and why (which module required it):

packt.addressbook requires packt.addressbook.lib file:///Users/koushik/code/java9/07-services/out/packt.addressbook.lib/
packt.addressbook requires java.logging jrt:/java.logging

It also figures out the service providers based on the modules that declared themselves as consumers:

packt.sortutil binds packt.sort.bubblesort file:///Users/koushik/code/java9/07-services/out/packt.sort.bubblesort/
packt.sortutil binds packt.sort.javasort file:///Users/koushik/code/java9/07-services/out/packt.sort.javasort/
packt.addressbook binds packt.sort.bubblesort file:///Users/koushik/code/java9/07-services/out/packt.sort.bubblesort/
packt.addressbook binds packt.sort.javasort file:///Users/koushik/code/java9/07-services/out/packt.sort.javasort/

The runtime continues to look for subsequent dependencies as it traverses the nodes of the module graph. After it's done adding all the necessary modules to the resolved set, it then executes the main method and the expected program output is printed on the console:

Aug 16, 2017 11:07:02 PM packt.addressbook.Main main
INFO: Address book viewer application: Started
[Charles Babbage, Tim Berners-Lee, Edsger Dijkstra, Ada Lovelace, Alan Turing]
Aug 16, 2017 11:07:03 PM packt.addressbook.Main main
INFO: Address book viewer application: Completed

Now that you've understood the process of module resolution and how it benefits the process of compile-time and runtime enforcement of reliable configuration, let's now look at another problem that it can solve. We briefly introduced the problem of the monolithic JDK in Chapter 1, Introducing Java 9 Modularity. We'll quickly recap the problem and then learn how that it is no longer a problem with Java 9.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141