Add automatic module name entry to MANIFEST.MF in java jar
In Java 9+, jars are supposed to have module names. If they don't (e.g., a jar built from an earlier version of Java), then Java will assign an automatic module name based on the file name. For pulse, the module name that gets created is pulse
. This is bad for at least two reasons:
- The name is considered "unstable" (because the file name could change), which leads to warnings
- It doesn't follow the naming convention. By the recommended conventions, the name should be
com.kitware.pulse
.
The module name is normally specified in module-info.java
, but this is only possible if you are compiling with Java 9+, and if you do that you will not be compatible with Java 8- (you can't just target a version earlier than 9 because of fundamental incompatibilities due to the module system).
Luckily, there is a way to specify what the module name should be for Java 9+ without affecting earlier versions of Java -- you can put it in the MANIFEST.MF file. This then becomes the "automatic" module name, as opposed to something derived from the file name. Java 9+ will look there for this info and use it, and Java 8- will ignore it, so it's the best of both worlds (at least until you're ready to leave Java 8 behind, which I assume is not any time soon).
The manifest file needs an entry like this:
Automatic-Module-Name: com.kitware.pulse
I manually modified the MANIFEST.MF file in Pulse.jar so it looks like this (I just added the last line):
Manifest-Version: 1.0
Created-By: 1.8.0_275 (Private Build)
Automatic-Module-Name: com.kitware.pulse
And that works! I'm not a cmake expert, but to get this to happen automatically, it looks like the file src/java/CMakeLists.txt
needs to be modified in the add_jar
block starting on line 25. Specifically, a clause like this can be added:
MANIFEST /path/to/manifest
With a custom manifest file like the one above provided.