Java - Module

Java Conceptuel Diagram

About

A Java module is a packaging format that create a modular JAR file.

It's a feature of JDK 9 from the Java Platform Module System (JPMS) that divide the monolithic rt.jar and tools.jar files into 75 distinct modules (at least in the OpenJDK, the Oracle JDK contained a few more).

Format and minimal example

Structure

The module consists of two type of source files:

  • the module declaration file: module-info.java
  • and the classes

By convention, the source code for the module is in a directory that is the name of the module.

src/com.greetings/com/greetings/Main.java
src/com.greetings/module-info.java

module-info.java

  • Minimal
module com.greetings { }
  • Exporting code to be used as a library (ie making it visible to other)
module com.greetings {
        exports com.greetings;
}
module com.socket {
        exports com.socket;
        exports com.socket.spi;
        uses com.socket.spi.NetworkSocketProvider;
}

Compile

With javac

Creation of the directory tree to get the output of the compilation

mkdir -p mods # the root module path
mkdir -p mods/com.greetings # the location of the module

The compilation

javac 
    -d mods/com.greetings \ # the target directory for the output of the compiled module
    --module-path mods \ # the module path permits to resolve external modules (ie code dependencies)
    src/com.greetings/module-info.java \
    src/com.greetings/com/greetings/Main.java

compile multiple modules with one javac command

mkdir mods
javac -d mods --module-source-path src $(find src -name "*.java")
package com.greetings;
public class Main {
        public static void main(String[] args) {
            System.out.println("Greetings!");
        }
}

Package (Modular Jar)

package - The contents of the compiled modules are exploded on the file system. For transportation and deployment purposes then it is usually more convenient to package a module as a modular JAR.

A modular JAR is a regular JAR file that has a module-info.class in its top-level directory.

mkdir mlib
# create [email protected]
jar --create --file=mlib/[email protected] \
      --module-version=1.0 -C mods/org.astro .
# create com.greetings.jar 
jar --create --file=mlib/com.greetings.jar \
     --main-class=com.greetings.Main -C mods/com.greetings .

Run

To run a modular jar

java -p mlib -m com.greetings

where:

  • m specifies the main module

To run raw class file exploded on the file system

java \
   --module-path mods \
   -m com.greetings/com.greetings.Main

where:

  • The m option specifies the main module, the value after the slash is the class name of the main class in the module.

Type

Multi-Release

Modular JAR files can also be Multi-Release JAR files - ie extend the JAR file format to allow multiple, Java-release-specific versions of class files to coexist in a single archive. (per JEP 238)

Management

Describe the module

print the module declaration for a module packaged as a modular JAR.

jar --describe-module --file=mlib/[email protected]

Tool

Linker

jlink is the linker tool and can be used to link a set of modules, along with their transitive dependences, to create a custom modular run-time image.

Gradle

Documentation / Reference





Discover More
Java Conceptuel Diagram
Java - Compile (class file)

The compilation create a class source.class file from a java source file ie source.java file. The create class language is called and can be run on any OS with any java executable (ie ) The...
Java Conceptuel Diagram
Java - Runtime Image

A runtime image permits to run Java class and have then at minimal a jvm (ie java) Prior to Java 9, there was only two fix run-time image JRE (a complete Java SE Platform implementation) JDK...
Java Conceptuel Diagram
Jlink to create a custom jvm image

jlink is a linker tool that can be used to create a custom modular run-time jvm image



Share this page:
Follow us:
Task Runner