3

I want to use Java 9 in my app. One of my dependencies has a jar name:

sdk-http-ahc-2_0

Unfortunately, when I try to change it to automatic module, the name for the module is not valid.

requires sdk.http.ahc.2_0; // not valid

Am I missing something with naming the module? What are my other options here?

Naman
  • 27,789
  • 26
  • 218
  • 353
Lojza Ibg
  • 658
  • 9
  • 32

1 Answers1

8

The problem is that the module system does not identify 2_0 as a version number and hence doesn't strip it when determining the automatic module name. Unfortunately, 2_0 is not a valid Java identifier and can hence not be used as a segment in a module name.

The solution is to either rename the JAR or add the Automatic-Module-Name entry to the JAR's manifest:

  1. Create a file manifest.txt with the following content:

    Automatic-Module-Name: sdk.http.ahc
    
  2. Then use jar to append that line to the existing manifest:

    jar --update --file sdk-http-ahc-2_0.jar --manifest=manifest.txt
    

Note that locally modifying existing JARs (name or manifest) can cause problems down the road. Consider changing the Maven version of the file to something like 2.0-patched-auto-name (or similar) and add it to your local Maven repository. If the project is shared with other developers and you have a local Nexus, you can put it in there. Otherwise send a mail to everyone with the steps to add it to their local repo.

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • Just on the renaming suggestion then rename it from sdk-http-ahc-2_0.jar to sdk-http-ahc-2.0.jar should work as it allows the module name to be derived as "sdk.http.ahc". – Alan Bateman Feb 10 '18 at 07:50
  • Thanks :) I was also thinking about the solution you proposed, but I was still hoping there's a better way. It's actually not a direct dependency, but a dependency of another dependency. With your solution, I would lose the option to update the library. I think I will create an issue in the creator's github repo and let the creator fix the name. – Lojza Ibg Feb 10 '18 at 09:04
  • "With your solution, I would lose the option to update the library." You mean your direct dependency? I don't think so. Your project could add an explicit dependency on `sdk-http-ahc-2_0` version `2.0-patched-auto-name`, which your build tool should then pick over the transitive dependency on the "broken" JAR. Still, opening an issue is definitely the right way to go in the long term. – Nicolai Parlog Feb 10 '18 at 14:00