Modular Programming in Java 9 - Build large scale applications using Java modularity and Project Jigsaw

Last updated Jun 10, 2023 Published Jun 10, 2023

The content here is under the Attribution 4.0 International (CC BY 4.0) license

  • learn how java 9 modules work
    • modules features
    • impact on the java platform
    • and how to use modules in applications

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 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 default package without creating another one
    • before java 9 apps could use encapsulation at package level and class/method level
  • The problem until java 9 is that one developer can create a library that is internal but 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
  • tradicional apps are bundled in a single jar
  • java module accepted 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 behaviour 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 inter dependencies
  • differences between classpath and initialization in java 9 + modules

Content

  • creating a module with intelij is acomplished via the file and then new menu.
    • there are some gotchas that needs to be take into account
      • first, intellij when creating modules already create packages inside it with test 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.
  • compiling two modules will give an error of cannot find symbol, but the compilation works independently.
  • having class in a module as public does not imply another modules 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 is a concept for many decades and now is not needed
    • given the modules path the compiler 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
  • 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 developer’s attention to not write programs that import sun.* packages
  • Project jigsaw was made to bring modularity to java
    • internal classes were grouped in 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 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 java fx
  • 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 describes 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

  • First step is to get java 9 or greater and compile the code
  • 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 (readability and accessibility [[#^f2bd0f]])
  • 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 in order to add this possibility
        • –add-exports
        • –add-opens
        • –permit-illegal-access -> disable all modularity features
    • the unnamed module is referenced by ALL-UNNAMED

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