SlideShare una empresa de Scribd logo
1 de 20
Save 42% off The Java Module
System by Nicolai Parlog with code
slparlog at manning.com
Connecting the pieces
Modules are the atomic building blocks, the nodes in
our graph of interacting artifacts. But there can be no
graph without edges connecting the nodes! So the
modules express which other modules they depend on,
creating edges between them.
The next slide shows a graphic of the types of JVM
modules:
Need some background info on Java 9 modules? Check
out the author’s website:
http://blog.codefx.org/tag/jpms/
Observable
Unnamed
Named
Platform
Application
Automatic
Base
Initial
Types of modules
Let’s imagine that we have a module customer and a
module bar.
When a module customer requires module bar in its
declaration, then at run time customer will read bar or,
conversely, bar will be readable by customer.
barcustomer
1. requires bar
2. reads
With that in mind, you can see how phrases like
"customer requires bar" and "customer depend on bar“
mirror a static, compile-time relationship between
customer and bar; readability is the more dynamic,
run-time counterpart.
You might be asking: Why is it more dynamic?
While the requires clause is the primal originator of
readability edges, it is by no means the only one – both
command line arguments and the reflection API can be
used to add more.
However they came to be, readability edges are the basis
for reliable configuration and strong encapsulation.
Achieving reliable configuration
Reliable configuration aims to ensure that the particular
configuration of artifacts a Java program is compiled
against, or launched with, can sustain the program
without spurious run-time errors.
To this end it performs a couple of checks – which
happen during module resolution.
The next slide shows the verification sequence.
1. First and foremost, the module system checks whether the universe of
observable modules contains all required dependencies (both direct and
transitive) and reports an error if something is missing.
2. There must be no ambiguity, which means that no two artifacts can claim
that they are the same module. This is particularly interesting in the case
where two versions of the same module are present—unfortunately the
module system has no concept of versions and thus treats this as a
duplicate module. Accordingly, you get an error if it runs into this
situation.
3. There must be no static dependency cycles between modules. At run time it
is possible and even necessary for modules to access each other, but these
must not be compile dependencies.
4. Packages should have a unique origin so no two modules contain types in
the same package. If they do, this is called a split package and the module
system will refuse to compile or launch such configurations.
This verification is, of course, not airtight and it is still
possible for problems to hide long enough to crash a
running application. If, for example, the wrong version
of a module ends up in the right place, then the
application will still launch (all required modules are
present after all) but crash later when, for example, a
class or method is missing.
The module system is developed to exhibit consistent
behavior across compile and run time, though, so these
errors can be further minimized if it can be guaranteed
that compilation and launch are based on the same
artifacts (for example, a compilation against a module
with the wrong version would fail).
Unreliable configurations
Now that you know how the module system is supposed
to work (and when it won’t), let’s see if we can break
things – so you know what to do when things go wrong
(they will).
Imagine that we have a simple monitor application, and
consider monitor.observer.alpha and its declaration:
module monitor.observer.alpha {
requires monitor.observer;
exports monitor.observer.alpha;
}
Missing dependencies
Now let’s see what it would look like to try to compile it
with monitor.observer missing:
Missing dependency, no compilation!
monitor.observer.alpha/src/main/java/module-info:2:
error: module not found: monitor.observer
requires monitor.observer
^
1 error
A different error would get thrown if the module was
present at compile time, but got lost on the way to the
launch pad. In this instance, the JVM will quit with the
following:
Missing dependencies
Error occurred during initialization of VM
java.lang.module.ResolutionException:
Module monitor.observer not found,
required by monitor.observer.alpha
Duplicate modules
Since modules reference one another by name, any
situation where two modules claim to have the same
name is ambiguous. Which one is the correct one to
pick is highly dependent on the context and not
something the module system can generally decide.
So, instead of making a potentially bad decision, it
makes none at all and instead produces an error.
This commitment to failing fast allows the developer to
notice a problem and fix it before it causes any more
problems.
Duplicate modules
This is the compile error the module system produces
when trying to compile a module with two variants of
monitor.observer.beta on the module path:
The compiler cannot link the error to one of the files
under compilation because they are not the reason for
the problem. In this case, the artifacts on the module
path are causing the error.
error: duplicate module on application module path
module in monitor.observer.beta
1 error
Duplicate modules
In the event of an error going undetected until the JVM
is launched, we get a more precise message that lists
the JAR file names as well:
As we mentioned before, the module system has no
concept of versions – many errors can be traced to
having several versions of the same module on the
module path.
Error occurred during initialization of VM
java.lang.module.ResolutionException:
Two versions of module monitor.observer.beta found in mods
(monitor.observer.beta.jar and monitor.observer.gamma.jar)
Dependency cycles
Getting dependency cycles through the compiler takes
some doing, and, if not done properly, will result in a
compile error.
This is how it looks if monitor.persistence and
monitor.statistics depend on each other:
monitor.statistics/src/main/java/module-info:3:
error: cyclic dependence involving monitor.persistence
requires monitor.persistence;
^
1 error
Split packages
A split package occurs when two modules contain types
in the same package. For example, the monitor.statistics
module contains a class Statistician in the
monitor.statistics package.
Now let’s assume the monitor module contained a very
simple fallback implementation SimpleStatistician,
and to promote uniformity, the monitor module has
placed it in its own monitor.statistics package.
The error we would get at compile time is on the next
slide:
Split packages
Interestingly enough, the compiler only shows an error if
the module under compilation can access the split
package in the other module, which means the split
package must be exported.
monitor/src/main/java/monitor/statistics/SimpleStatistician.java:1:
error: package exists in another module: monitor.statistics
package monitor.statistics;
^
1 error
Split packages
Let’s see this another way: SimpleStatistician is
gone and this time monitor.statistics creates the split
module. It gets a Utils class in the monitor package,
and continues to only export the monitor.statistics
package. Compiling monitor.statistics is error-free, which
makes sense because it does not require monitor.
Compiling monitor is more interesting because it
depends on monitor.statistics and both contain types in
the package monitor. But because monitor.statistics
does not export the split package, compilation is
successful.
Split packages
That didn’t go so well… Indeed, the module system
checks for split packages on launch and here it doesn’t
matter whether they are exported or not: There can be
no two modules that contain types in the same package.
Error occurred during initialization of boot layer
java.lang.reflect.LayerInstantiationException:
Package monitor in both module monitor.statistics and module monitor
Let’s see what happens when we launch:
Grab your copy of The Java Module System and save
42% with code slparlog at manning.com.
Also see:

Más contenido relacionado

Más de Manning Publications

Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...Manning Publications
 
Entity Framework Core in Action, Second Edtion
Entity Framework Core in Action, Second EdtionEntity Framework Core in Action, Second Edtion
Entity Framework Core in Action, Second EdtionManning Publications
 
Microservices in .NET Core, Second Edition
Microservices in .NET Core, Second EditionMicroservices in .NET Core, Second Edition
Microservices in .NET Core, Second EditionManning Publications
 
Kubernetes in Action, Second Edition
Kubernetes in Action, Second EditionKubernetes in Action, Second Edition
Kubernetes in Action, Second EditionManning Publications
 
Machine Learning with TensorFlow, Second Edition
Machine Learning with TensorFlow, Second EditionMachine Learning with TensorFlow, Second Edition
Machine Learning with TensorFlow, Second EditionManning Publications
 
Spring Microservices in Action, Second Edition
Spring Microservices in Action, Second EditionSpring Microservices in Action, Second Edition
Spring Microservices in Action, Second EditionManning Publications
 
Grokking Artificial Intelligence Algorithms
Grokking Artificial Intelligence AlgorithmsGrokking Artificial Intelligence Algorithms
Grokking Artificial Intelligence AlgorithmsManning Publications
 
GitOps and Kubernetes: a radical idea
GitOps and Kubernetes: a radical ideaGitOps and Kubernetes: a radical idea
GitOps and Kubernetes: a radical ideaManning Publications
 
Data Pipelines with Apache Airflow
Data Pipelines with Apache AirflowData Pipelines with Apache Airflow
Data Pipelines with Apache AirflowManning Publications
 
Learn PowerShell in a Month of Lunches: Linux and macOS Edition
Learn PowerShell in a Month of Lunches: Linux and macOS EditionLearn PowerShell in a Month of Lunches: Linux and macOS Edition
Learn PowerShell in a Month of Lunches: Linux and macOS EditionManning Publications
 
Hugo in Action: website creation made painless
Hugo in Action: website creation made painlessHugo in Action: website creation made painless
Hugo in Action: website creation made painlessManning Publications
 
Learn dbatools in a Month of Lunches
Learn dbatools in a Month of LunchesLearn dbatools in a Month of Lunches
Learn dbatools in a Month of LunchesManning Publications
 
Designing APIs with Swagger and OpenAPI
Designing APIs with Swagger and OpenAPIDesigning APIs with Swagger and OpenAPI
Designing APIs with Swagger and OpenAPIManning Publications
 
PostGIS in Action, Third Edition: the newly-updated guide
PostGIS in Action, Third Edition: the newly-updated guidePostGIS in Action, Third Edition: the newly-updated guide
PostGIS in Action, Third Edition: the newly-updated guideManning Publications
 

Más de Manning Publications (20)

Cloud Native Machine Learning
Cloud Native Machine Learning Cloud Native Machine Learning
Cloud Native Machine Learning
 
Spring in Action, Sixth Edition
Spring in Action, Sixth EditionSpring in Action, Sixth Edition
Spring in Action, Sixth Edition
 
Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...
 
Entity Framework Core in Action, Second Edtion
Entity Framework Core in Action, Second EdtionEntity Framework Core in Action, Second Edtion
Entity Framework Core in Action, Second Edtion
 
Code like a Pro in C#
Code like a Pro in C#Code like a Pro in C#
Code like a Pro in C#
 
Microservices in .NET Core, Second Edition
Microservices in .NET Core, Second EditionMicroservices in .NET Core, Second Edition
Microservices in .NET Core, Second Edition
 
Kubernetes in Action, Second Edition
Kubernetes in Action, Second EditionKubernetes in Action, Second Edition
Kubernetes in Action, Second Edition
 
Core Kubernetes
Core KubernetesCore Kubernetes
Core Kubernetes
 
Machine Learning Bookcamp
Machine Learning BookcampMachine Learning Bookcamp
Machine Learning Bookcamp
 
Machine Learning with TensorFlow, Second Edition
Machine Learning with TensorFlow, Second EditionMachine Learning with TensorFlow, Second Edition
Machine Learning with TensorFlow, Second Edition
 
Spring Security in Action
Spring Security in ActionSpring Security in Action
Spring Security in Action
 
Spring Microservices in Action, Second Edition
Spring Microservices in Action, Second EditionSpring Microservices in Action, Second Edition
Spring Microservices in Action, Second Edition
 
Grokking Artificial Intelligence Algorithms
Grokking Artificial Intelligence AlgorithmsGrokking Artificial Intelligence Algorithms
Grokking Artificial Intelligence Algorithms
 
GitOps and Kubernetes: a radical idea
GitOps and Kubernetes: a radical ideaGitOps and Kubernetes: a radical idea
GitOps and Kubernetes: a radical idea
 
Data Pipelines with Apache Airflow
Data Pipelines with Apache AirflowData Pipelines with Apache Airflow
Data Pipelines with Apache Airflow
 
Learn PowerShell in a Month of Lunches: Linux and macOS Edition
Learn PowerShell in a Month of Lunches: Linux and macOS EditionLearn PowerShell in a Month of Lunches: Linux and macOS Edition
Learn PowerShell in a Month of Lunches: Linux and macOS Edition
 
Hugo in Action: website creation made painless
Hugo in Action: website creation made painlessHugo in Action: website creation made painless
Hugo in Action: website creation made painless
 
Learn dbatools in a Month of Lunches
Learn dbatools in a Month of LunchesLearn dbatools in a Month of Lunches
Learn dbatools in a Month of Lunches
 
Designing APIs with Swagger and OpenAPI
Designing APIs with Swagger and OpenAPIDesigning APIs with Swagger and OpenAPI
Designing APIs with Swagger and OpenAPI
 
PostGIS in Action, Third Edition: the newly-updated guide
PostGIS in Action, Third Edition: the newly-updated guidePostGIS in Action, Third Edition: the newly-updated guide
PostGIS in Action, Third Edition: the newly-updated guide
 

Último

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 

Último (20)

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 

The Java Module System: reliably configuring Java 9 modules

  • 1. Save 42% off The Java Module System by Nicolai Parlog with code slparlog at manning.com
  • 2. Connecting the pieces Modules are the atomic building blocks, the nodes in our graph of interacting artifacts. But there can be no graph without edges connecting the nodes! So the modules express which other modules they depend on, creating edges between them. The next slide shows a graphic of the types of JVM modules: Need some background info on Java 9 modules? Check out the author’s website: http://blog.codefx.org/tag/jpms/
  • 4. Let’s imagine that we have a module customer and a module bar. When a module customer requires module bar in its declaration, then at run time customer will read bar or, conversely, bar will be readable by customer. barcustomer 1. requires bar 2. reads
  • 5. With that in mind, you can see how phrases like "customer requires bar" and "customer depend on bar“ mirror a static, compile-time relationship between customer and bar; readability is the more dynamic, run-time counterpart. You might be asking: Why is it more dynamic? While the requires clause is the primal originator of readability edges, it is by no means the only one – both command line arguments and the reflection API can be used to add more. However they came to be, readability edges are the basis for reliable configuration and strong encapsulation.
  • 6. Achieving reliable configuration Reliable configuration aims to ensure that the particular configuration of artifacts a Java program is compiled against, or launched with, can sustain the program without spurious run-time errors. To this end it performs a couple of checks – which happen during module resolution. The next slide shows the verification sequence.
  • 7. 1. First and foremost, the module system checks whether the universe of observable modules contains all required dependencies (both direct and transitive) and reports an error if something is missing. 2. There must be no ambiguity, which means that no two artifacts can claim that they are the same module. This is particularly interesting in the case where two versions of the same module are present—unfortunately the module system has no concept of versions and thus treats this as a duplicate module. Accordingly, you get an error if it runs into this situation. 3. There must be no static dependency cycles between modules. At run time it is possible and even necessary for modules to access each other, but these must not be compile dependencies. 4. Packages should have a unique origin so no two modules contain types in the same package. If they do, this is called a split package and the module system will refuse to compile or launch such configurations.
  • 8. This verification is, of course, not airtight and it is still possible for problems to hide long enough to crash a running application. If, for example, the wrong version of a module ends up in the right place, then the application will still launch (all required modules are present after all) but crash later when, for example, a class or method is missing. The module system is developed to exhibit consistent behavior across compile and run time, though, so these errors can be further minimized if it can be guaranteed that compilation and launch are based on the same artifacts (for example, a compilation against a module with the wrong version would fail).
  • 9. Unreliable configurations Now that you know how the module system is supposed to work (and when it won’t), let’s see if we can break things – so you know what to do when things go wrong (they will). Imagine that we have a simple monitor application, and consider monitor.observer.alpha and its declaration: module monitor.observer.alpha { requires monitor.observer; exports monitor.observer.alpha; }
  • 10. Missing dependencies Now let’s see what it would look like to try to compile it with monitor.observer missing: Missing dependency, no compilation! monitor.observer.alpha/src/main/java/module-info:2: error: module not found: monitor.observer requires monitor.observer ^ 1 error
  • 11. A different error would get thrown if the module was present at compile time, but got lost on the way to the launch pad. In this instance, the JVM will quit with the following: Missing dependencies Error occurred during initialization of VM java.lang.module.ResolutionException: Module monitor.observer not found, required by monitor.observer.alpha
  • 12. Duplicate modules Since modules reference one another by name, any situation where two modules claim to have the same name is ambiguous. Which one is the correct one to pick is highly dependent on the context and not something the module system can generally decide. So, instead of making a potentially bad decision, it makes none at all and instead produces an error. This commitment to failing fast allows the developer to notice a problem and fix it before it causes any more problems.
  • 13. Duplicate modules This is the compile error the module system produces when trying to compile a module with two variants of monitor.observer.beta on the module path: The compiler cannot link the error to one of the files under compilation because they are not the reason for the problem. In this case, the artifacts on the module path are causing the error. error: duplicate module on application module path module in monitor.observer.beta 1 error
  • 14. Duplicate modules In the event of an error going undetected until the JVM is launched, we get a more precise message that lists the JAR file names as well: As we mentioned before, the module system has no concept of versions – many errors can be traced to having several versions of the same module on the module path. Error occurred during initialization of VM java.lang.module.ResolutionException: Two versions of module monitor.observer.beta found in mods (monitor.observer.beta.jar and monitor.observer.gamma.jar)
  • 15. Dependency cycles Getting dependency cycles through the compiler takes some doing, and, if not done properly, will result in a compile error. This is how it looks if monitor.persistence and monitor.statistics depend on each other: monitor.statistics/src/main/java/module-info:3: error: cyclic dependence involving monitor.persistence requires monitor.persistence; ^ 1 error
  • 16. Split packages A split package occurs when two modules contain types in the same package. For example, the monitor.statistics module contains a class Statistician in the monitor.statistics package. Now let’s assume the monitor module contained a very simple fallback implementation SimpleStatistician, and to promote uniformity, the monitor module has placed it in its own monitor.statistics package. The error we would get at compile time is on the next slide:
  • 17. Split packages Interestingly enough, the compiler only shows an error if the module under compilation can access the split package in the other module, which means the split package must be exported. monitor/src/main/java/monitor/statistics/SimpleStatistician.java:1: error: package exists in another module: monitor.statistics package monitor.statistics; ^ 1 error
  • 18. Split packages Let’s see this another way: SimpleStatistician is gone and this time monitor.statistics creates the split module. It gets a Utils class in the monitor package, and continues to only export the monitor.statistics package. Compiling monitor.statistics is error-free, which makes sense because it does not require monitor. Compiling monitor is more interesting because it depends on monitor.statistics and both contain types in the package monitor. But because monitor.statistics does not export the split package, compilation is successful.
  • 19. Split packages That didn’t go so well… Indeed, the module system checks for split packages on launch and here it doesn’t matter whether they are exported or not: There can be no two modules that contain types in the same package. Error occurred during initialization of boot layer java.lang.reflect.LayerInstantiationException: Package monitor in both module monitor.statistics and module monitor Let’s see what happens when we launch:
  • 20. Grab your copy of The Java Module System and save 42% with code slparlog at manning.com. Also see: