Michael Remijan
Michael Remijan

Reputation: 707

Java modules and the JAR MANIFEST Class-Path attribute

I have a Java 8 application I am working on converting to Java 11 and of course with this I am working with the module system for the first time.

I have my application Maven assembly configuration set such that all the JAR files are put in a /lib directory of the bin distribution. The maven-jar-plugin then sets the MANIFEST Class-Path value with all the JAR file names. The application is run with a simple java -jar /lib/application.jar

Moving to Java 11, if I continue to build the bin distribution this way, I'm curious to know how the runtime interprets the MANIFEST Class-Path value? I read through the JAR specs (https://docs.oracle.com/en/java/javase/11/docs/specs/jar/jar.html) and I think I understand it as:

  1. Any modular JAR file (contains module-info.class) will be treated as a non-modular JAR.
  2. Any non-modular JAR file (no module-info.class) will be treated as a non-modular JAR.
  3. Since everything is on the Class-Path, everything is essentially an unnamed module
  4. Since everything is on the Class-Path, the module system is essentially not being used at runtime
  5. Since everything is on the Class-Path, the application should run essentially as if it's still running as Java 8?

If I did want to change this, would I start putting any modular JAR file on the --module-path and keep the other JAR files on the Class-Path?

Upvotes: 2

Views: 3314

Answers (1)

Thiago Henrique Hupner
Thiago Henrique Hupner

Reputation: 492

Exactly; Running via -jar all your code is in the unnamed module, while the JDK classes themselves are running on modules, that's why you can see "java.base/" in the stacktraces running with JDK 9+.

So your invocation would have to change from java -jar lib/application.jar to java --module-path lib --module application/your.main.class

You can improve it further by using JLink and/or JPackage, where you can create an executable that already executes your module, like ./bin/application.

Start by adding all jars to the module path and see if the Java doesn't say anything about split packages. So by doing that, it almost certain that your modules will be "automatic modules". So you can experience some ClassNotFoundException.

If that happens, you'll need to --add-module module.

Upvotes: 3

Related Questions