Sub topic 15
Split packages and how to fix them
This chapter assumes you can write a module-info.java, compile with --module-source-path, and run from the module path. You should also know the difference between named, automatic, and unnamed modules from the previous chapters.
What a split package is
A split package is a package name that appears in more than one module. For example, com.example.util is a split package if lib-a.jar and lib-b.jar both contain classes in com.example.util.
On the classpath this is legal. The class loader searches jars in order and loads each class from the first jar that has it. The module system forbids it, because a package must belong to exactly one module. This rule lets the runtime map a package name to the module that owns it, so it can check readability and encapsulation.
The rules that matter
- A named module cannot read two modules that both contain the same package, exported or not, if they share a class loader. Application modules in the boot layer all share the application class loader.
- A module can also read two modules that export the same package to it. That fails too.
- An automatic module exports all its packages and reads every other module. Two jars with overlapping packages therefore conflict as soon as both are on the module path.
- If a named module and the classpath contain the same package, the named module wins. The classpath copy is silently invisible, which causes confusing
NoClassDefFoundErrorfailures. - Packages in JDK modules count too. A library that declares
javax.annotationcan clash with a JDK module that owns that package.
How the errors look
At compile time, javac reports the clash like this:
error: package exists in another module: com.example.aAt run time you see one of two exceptions. Exact wording varies by JDK version.
java.lang.module.ResolutionException:
Module com.example.app reads package com.example.util
from both com.example.a and com.example.b
java.lang.LayerInstantiationException:
Package com.example.util in both module com.example.a and module com.example.bThe first exception comes from resolution. The second comes from creating the layer, when two modules mapped to one class loader contain the same package.
A broken example
Both modules below own com.example.util.
// src/com.example.a/module-info.java
module com.example.a {
exports com.example.util;
}
// src/com.example.a/com/example/util/Strings.java
package com.example.util;
public class Strings {
public static String shout(String s) { return s.toUpperCase(); }
}// src/com.example.b/module-info.java
module com.example.b {
exports com.example.util;
}
// src/com.example.b/com/example/util/Numbers.java
package com.example.util;
public class Numbers {
public static int twice(int n) { return n * 2; }
}// src/com.example.app/module-info.java
module com.example.app {
requires com.example.a;
requires com.example.b;
}
// src/com.example.app/com/example/app/Main.java
package com.example.app;
import com.example.util.Numbers;
import com.example.util.Strings;
public class Main {
public static void main(String[] args) {
System.out.println(Strings.shout("split") + " " + Numbers.twice(21));
}
}Compiling with javac -d out --module-source-path src -m com.example.app fails because com.example.app reads two modules that export com.example.util.
Fixing your own code
When you control both modules, you have two options.
- Rename one package. Give each module a package prefix that matches its module name, such as
com.example.a.utilandcom.example.b.util. This is the usual fix and a good naming convention anyway. - Merge the modules. If the two modules are really one library, put them in one module.
The renamed version of module b looks like this:
// src/com.example.b/module-info.java
module com.example.b {
exports com.example.b.util;
}
// src/com.example.b/com/example/b/util/Numbers.java
package com.example.b.util;
public class Numbers {
public static int twice(int n) { return n * 2; }
}Then update the import in Main to com.example.b.util.Numbers. The same example now compiles and runs.
Fixing third-party jars
You cannot rename packages in someone else's jar, so the options differ.
| Situation | Fix |
|---|---|
| Two versions of the same library are present | Remove one. Align the versions in your build tool (Maven or Gradle dependency convergence) so only one jar remains. |
| Two different libraries bundle the same classes (often a "fat" jar plus the original) | Exclude one dependency, or depend on the non-bundled artifact. |
| A library overlaps with a JDK module | Upgrade to a modular release, or replace it with the dependency that the JDK removed (for example, a standalone artifact). |
| No fix is possible | Leave the jars on the classpath, where they belong to the unnamed module and split packages are allowed. |
| You must add classes to an existing module, such as for testing | Use `--patch-module`. |
| The library is unmaintained | Relocate its packages with a shading tool (for example Maven Shade relocation) and publish your own jar. |
--patch-module merges the contents of a directory or jar into one named module at compile or run time. It is meant for tests and small workarounds, not for production layouts:
java --patch-module com.example.a=test-classes \
--module-path out \
-m com.example.app/com.example.app.MainFinding split packages before you migrate
This script lists every package that appears in more than one jar under libs/:
for j in libs/*.jar; do
jar tf "$j" | grep '/.*\.class$' | grep -v '^META-INF' \
| sed 's|/[^/]*$||' | sort -u | sed "s|\$| $(basename "$j")|"
done | sort | awk '{c[$1]++; j[$1]=j[$1] " " $2}
END {for (p in c) if (c[p]>1) print p ":" j[p]}'The script prints the package and the jars that contain it, such as com/example/util: lib-a.jar lib-b.jar. Package names appear with slashes because they come from jar paths. For a quick check of one jar, jar --describe-module --file lib-a.jar prints its packages (as contains entries for an automatic module).
Edge cases
- Identical classes in two jars. Even byte-identical copies are a split. The module system checks package names, not contents.
- Empty packages. A package with no classes does not count. Only packages that contain classes or resources can conflict.
- Multi-release jars. Entries under
META-INF/versionsbelong to the same module as the main jar and do not count as separate packages. - Different class loaders. Modules in separate layers with separate class loaders may share a package name. This is the basis of the dynamic-loading techniques covered in a later chapter. It is not a workaround for the boot layer.
- Test code. A test class in the same package as the code under test, but in a different module, is a split. Build tools solve this with
--patch-module, which merges the test classes into the main module.
Exercise
A project has lib-a.jar and lib-b.jar on the module path as automatic modules. Both contain classes in org.acme.json. The application is com.example.app, which requires both. The app fails at startup with a ResolutionException. Give two ways to fix it and say which one you would try first.
Show answer
Fix 1: remove one jar. Align the build so only one artifact provides org.acme.json. If one jar is a fat jar that bundles the other, depend on the unbundled artifact. Fix 2: leave the jars on the classpath, where the unnamed module allows split packages, and run com.example.app with both jars on -cp. Fix 3, if neither works: relocate one jar's packages with a shading tool. Try the dependency fix first, because it removes the duplication at its source and keeps the application fully modular. An automatic module exports every package and reads everything, so the overlap is guaranteed to conflict. Renaming the package is not an option because the jars are third-party.