JAR and runtime images
What you will learn
A JAR bundles class files and resources; a runtime image bundles selected JDK modules and optionally application modules. jpackage assembles a deployable application image or platform installer.
- Distinguish ordinary JARs, modular JARs and runtime images.
- Use jar, jdeps, jlink and jpackage for a small project.
- Explain portability and deployment trade-offs.
Each example is a complete Main.java file. Use a fresh lab-owned directory: javac --release 21 -proc:none Main.java, then java Main. Use only trusted local classes and fixed synthetic data. For module commands use the companion lab described below. Never load unknown native code or attach to another process. The runner enforces a process deadline.
Check whether a descriptor exists
public class Main {
public static void main(String[] args) throws Exception { var d = String.class.getModule().getDescriptor(); System.out.println(d != null); System.out.println(d.isAutomatic()); }}Expected output:
truefalseThe base module has an explicit descriptor, not an automatic one.
Package an application
jar --create --file jars/lab.app.jar --main-class lab.app.Main -C mods/lab.app . packages the app module. For the companion lab also package lab.api and lab.provider as separate JARs in jars/. jar --describe-module --file jars/lab.app.jar inspects the descriptor. Launch with java --module-path jars --module lab.app/lab.app.Main. An ordinary executable JAR instead uses java -jar app.jar; its manifest Main-Class is not a dependency resolver.
Build a custom runtime
jdeps --module-path jars --module lab.app --print-module-deps reports static module dependencies; reflection, configuration and service providers can need explicit handling. jlink --module-path "$JAVA_HOME/jmods:jars" --add-modules lab.app,lab.provider --launcher catalog=lab.app/lab.app.Main --output image creates a Linux/macOS runtime image in an absent destination. Windows path separator is semicolon. Run image/bin/catalog. Explicit provider inclusion avoids assuming static dependency analysis will find the service. --bind-services is an alternative that can add more providers. Link with the intended JDK/platform; rebuild images for security updates. Automatic modules are not supported by JDK 21 jlink.
Deploy without an installed JDK
jpackage --type app-image --name ByteMindLab --module-path jars --module lab.app/lab.app.Main --runtime-image image --dest dist uses the linked runtime. It does not create an installer in app-image mode. Installer formats and prerequisites depend on the target OS; build and sign for each target, test upgrades and uninstall separately. A desktop app may require java.desktop and GUI tests. The lab tests a console launcher only. Neither jlink nor jpackage makes an app automatically cross-platform.
Read a manifest without executing a JAR
public class Main {
public static void main(String[] args) throws Exception { var manifest = new java.util.jar.Manifest(); var a = manifest.getMainAttributes(); a.put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.0"); a.put(java.util.jar.Attributes.Name.MAIN_CLASS, "lab.app.Main"); System.out.println(a.getValue("Main-Class")); }}Expected output:
lab.app.MainA manifest can declare a main class; this in-memory example does not package its dependencies.
Create and inspect a tiny resource JAR
public class Main {
public static void main(String[] args) throws Exception { var file = java.nio.file.Files.createTempFile("lab-", ".jar"); try { try (var jar = new java.util.jar.JarOutputStream(java.nio.file.Files.newOutputStream(file))) { jar.putNextEntry(new java.util.jar.JarEntry("greeting.txt")); jar.write("hello".getBytes(java.nio.charset.StandardCharsets.UTF_8)); jar.closeEntry(); } try (var jar = new java.util.jar.JarFile(file.toFile()); var in = jar.getInputStream(jar.getJarEntry("greeting.txt"))) { System.out.println(new String(in.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8)); } } finally { java.nio.file.Files.deleteIfExists(file); } }}Expected output:
helloThis owned five-byte resource demonstrates archive access, not safe arbitrary ZIP extraction.
Common mistakes
Do not ship only the application JAR while assuming dependencies are bundled, assume jdeps finds all dynamic access, or reuse an old runtime image after a security update.
Practice
Create an in-memory manifest with Main-Class lab.demo.Start and print it. In the companion project, compare the ordinary module-path launch with the linked runtime launcher.
Expected output:
lab.demo.StartCompare your answer with the manual practice criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JAR and runtime images — knowledge check
Original module, packaging and native-interface questions.
What does a JAR contain?
What is jlink for?
Why can jdeps miss a dependency?
What does jpackage --type app-image create?
How should an image receive JDK security fixes?