Compilation, JARs, and Runtime Images in Java

Master compiling Java code, creating modular and non-modular JARs, runtime images (jlink), migration to modules, unnamed modules, and automatic modules for the OCP 21 exam.

Table of Contents

1. Compiling Modules

1.1 Compiling Single Module

Example:
// Compile module
javac -d mods/com.example.mymodule \
    src/com.example.mymodule/module-info.java \
    src/com.example.mymodule/com/example/mymodule/*.java

// Run module
java --module-path mods -m com.example.mymodule/com.example.mymodule.Main

1.2 Compiling Multiple Modules

Example:
// Compile module with dependencies
javac --module-path mods \
    -d mods/com.example.mymodule \
    src/com.example.mymodule/module-info.java \
    src/com.example.mymodule/com/example/mymodule/*.java

// --module-path: where to find dependent modules
// -d: output directory

2. Modular JARs

A modular JAR is a JAR file that contains a module-info.class file in its root.

2.1 Creating Modular JAR

Example:
// Create modular JAR
jar --create --file=lib/mymodule.jar \
    --module-version=1.0 \
    -C mods/com.example.mymodule .

// --create: create new JAR
// --file: output file name
// --module-version: module version
// -C: change directory before processing

// Run from modular JAR
java --module-path lib -m com.example.mymodule/com.example.mymodule.Main

3. Non-Modular JARs

Non-modular JARs are traditional JAR files without module-info.class. They become part of the unnamed module when placed on the classpath.

3.1 Creating Non-Modular JAR

Example:
// Create non-modular JAR (traditional way)
jar cvf myapp.jar -C classes .

// Run from classpath
java -cp myapp.jar com.example.Main

// Non-modular JARs on classpath become part of unnamed module

4. Runtime Images (jlink)

The jlink tool creates custom runtime images containing only the required modules.

4.1 Creating Runtime Image

Example:
// Create runtime image
jlink --module-path mods:$JAVA_HOME/jmods \
    --add-modules com.example.mymodule \
    --output myapp-image

// --module-path: where to find modules
// --add-modules: modules to include
// --output: output directory

// Run from runtime image
myapp-image/bin/java -m com.example.mymodule/com.example.mymodule.Main

// Runtime image contains only required modules (smaller size)

5. Migration to Modules

5.1 Unnamed Module

Code on the classpath (non-modular JARs) becomes part of the unnamed module. The unnamed module:

  • Reads all modules
  • Exports all packages
  • Cannot be required by named modules

5.2 Automatic Modules

Non-modular JARs placed on the module path become automatic modules. They:

  • Derive module name from JAR filename
  • Export all packages
  • Read all modules
  • Can be required by named modules
Example:
// JAR file: mylibrary-1.0.jar
// Automatic module name: mylibrary

// In module-info.java
module com.example.mymodule {
    requires mylibrary;  // Automatic module
}

6. Exam Key Points

Critical Concepts for OCP 21 Exam:

  • javac: Compile modules with --module-path
  • java: Run modules with --module-path and -m
  • jar: Create JARs with --create and --module-version
  • Modular JAR: Contains module-info.class
  • Non-Modular JAR: Traditional JAR without module-info.class
  • jlink: Create custom runtime images
  • Runtime Image: Contains only required modules
  • Unnamed Module: Code on classpath
  • Automatic Module: Non-modular JAR on module path
  • Module Path: --module-path for modules
  • Classpath: -cp or -classpath for non-modular code

Post a Comment

0 Comments