Modular Programming in Java 9 - Build large scale applications using Java modularity and Project Jigsaw
Last updated May 5, 2024
Published Jun 10, 2023
The content here is under the Attribution 4.0 International (CC BY 4.0) license
Discover the comprehensive guide to constructing expansive applications with Java modularity and Project Jigsaw and learn the steps in migrating to Java 9 modules by initiating code segmentation.
Chapter 1 - Introduction to Modules
Objectives
- pain points to maintain large code bases
- discuss modularity before Java 9
- The project jigsaw
- JPMS - Java Platform Module System
Content
- modular programming is a generally favored software design approach
- divide and conquer approach
- Achieving encapsulation and well-defined interfaces
- modules give the ability to hide internal implementation
- Java never had native support for modules
- Java 9 introduces a new concept of modules
- Java apps could work with the default package without creating another one
- Before Java 9 apps could use encapsulation at the package level and class/method level
- The problem until Java 9 is that one developer can create an internal library but the consumer could reference the internal implementation without any blocker
- there is no way for Java apps to know if a dependency is loaded until it is required to be used
- traditional apps are bundled in a single jar
Java module was accepted in JSR 376
- Official JSR 376
Chapter 2 - The first module
- Creating the first module from scratch
- IntelliJ creates extra things that the book example does not need
- The behavior of the application changes once a module is introduced - no more classes can be created without a package
Chapter 3 - Module dependencies
Objectives
- split code into two or more modules
- modules interdependencies
- differences between classpath and initialization in Java 9 + modules
Content
- creating a module with Intelij is accomplished via the file and then a new menu.
- some gotchas need to be taken into account
- first, IntelliJ when creating modules already creates packages inside it with tests and resources
- the module is created from the root of the project. If the module needs to be inside src there is no option in the menu to do so.
- some gotchas need to be taken into account
- compiling two modules will give an error of cannot find symbol, but the compilation works independently.
- having a class in a module as public does not imply another module can access it
- modules work with dependencies
- each module needs to declare which dependencies can be accessed externally
- and each module needs to declare what module it depends on
- requires and exports are keywords
- module version is not possible in JPMS
-
classpath has been a concept for many decades and now is not needed
- given the module path the compiler and the runtime can now know where all classes are
- when executing the code the check is not in run time anymore the check happens at the VM initialization
Chapter 4 - JDK with modules
Objectives
- modular jdk
- entire Java codebase has been modularized
- built-in modules
Content
- JRE has two folders
- bin
- executables
- lib
- key jar files for java
- rt.jar contains almost all the classes needed for the runtime
- bin
- having a big rt.jar file means shipping big files
- sometimes the application does not use all of it
- java 8 used to use compact
- compact uses 11bm
- internal vs public APIs
- unsafe from SUN was never meant to be used publicly
- Java is a backwards-compatible platform and it comes with a cost
-
attempt by SUN was made to bring the developer’s attention to not write programs that import
sun.*
packages
- Project jigsaw was made to bring modularity to Java
- internal classes were grouped into different modules
- java.scripting
- java.desktop
- java.transaction
- there is no need to import classes from java.lang java platform does that automatically
- java –list-modules to see the available modules in java
- observable module
Chapter 5 - using APIs from the Java platform
Objectives
- hands-on creating other modules for:
- adding logging (logging from java
java.logging
) - reading contacts from XML
- Dependencies should be a directed acyclic graph - no circular dependencies
- adding UI support with JavaFx
- adding logging (logging from java
- without requiring the modules the app won’t work
Chapter 6 - Module resolution
Objectives
- dive into nuances of readability of modules, resolution and accessibility
Content
- readability
- describes how each module work with each other
- cyclic dependencies are not allowed
- module dependency graphs describe the relationships between the modules and the order of loading
- accessibility
Chapter 10 - migrating to Java 9 modules
Objectives
- path of migration for legacy code bases
Content
- The first step is to get Java 9 or greater and compile the code
-
The second step is to start splitting the code to use modules
- surprisingly enough the code that was shared works with Java 9 even without using modules
- java has this behavior to support legacy code bases
- this is also known as unnamed module
- the unnamed module
- it gives access to all the classes and packages by default
- jdeps - Java dependency analysis tool
- statically examines code to identify usage of internal API classes
- tricks with module exports for apps without module
- java provides overrides to support migration
- if a given code does not have access to a module parameters can be given to the compiler to add this possibility
- –add-exports
- –add-opens
- –permit-illegal-access -> disable all modularity features
- if a given code does not have access to a module parameters can be given to the compiler to add this possibility
- the unnamed module is referenced by ALL-UNNAMED
- java provides overrides to support migration
Chapter 11 - migration to java 9
Objectives
- migrate strategy for legacy code
- Multi release jars
- module bit by bit an application
Content
- application in the real world are complex
- gradually migration is preferred
- three layers
- application
- frameworks
- java platform
References
Table of contents
Got a question?
If you have question or feedback, don't think twice and click here to leave a comment. Just want to support me? Buy me a coffee!