Course contentsShow
← Java
Lesson 2519 of 2,87047. Java Platform Module System (JPMS)Free lesson

Mixing Module Path and Classpath: Compatibility Mode

Understand how modular and non-modular code can coexist, and when the JVM uses classpath vs module path.

Mixing Module Path and Classpath: Compatibility Mode

You'll learn how the JVM handles code living in both the modular and non-modular worlds simultaneously.

The Core Idea

In real-world projects, you often need to use legacy libraries (non-modular JARs) alongside your new modular code. Java's compatibility mode allows this by treating classpath JARs as a special unnamed module that can read all other modules and exports all its packages—but only to other classpath code.

Think of it like a legacy building (classpath) next to a modern apartment complex (module path). The old building has universal access to everything, but the modern apartments have strict access rules that don't automatically trust the old building.

How It Works

When you launch a Java application:

  • Module path (--module-path): Contains modular JARs with module-info.class. The JVM enforces strong encapsulation and readability rules.
  • Classpath (--class-path): Contains non-modular JARs. These become part of the unnamed module.

The unnamed module:

  • Reads all named modules automatically
  • Exports all its packages, but only to other unnamed module code
  • Cannot be referenced in requires directives (you can't explicitly depend on it)

Why This Matters

Your modular code can access classpath libraries because the unnamed module reads everything. However, you cannot make classpath code required by your module declarations—the unnamed module stays implicit and invisible to JPMS.

This compatibility ensures you can gradually migrate to modules without rewriting all dependencies at once.

Key Takeaway: The JVM's compatibility mode lets modular (module path) and non-modular (classpath) code coexist by placing classpath JARs in an unnamed module that reads all modules but cannot be explicitly referenced in module declarations.