How to use third-party library in Java9 module?

I have a java9 module that uses a third-party library that is not a Java9 module, just a simple jar utility.

However, the compiler complains that it cannot find the package from my utility.

What should I do in module-info.java to enable the use of my third-party library?

+9
java java-9 modularity jigsaw
source share
3 answers

You can use your library as an automatic module. An automatic module is a module that does not have a module descriptor (i.e. module-info.class ). But what name should you use to refer to this module? The name of the automatic module is derived from the JAR name (unless that JAR contains the Automatic-Module-Name attribute). The complete rule is quite long (see Javadoc for ModuleFinder.of ), so for simplicity you just need to drop the version from your name and then replace all non-alphanumeric characters with periods ( . ).

For example, if you want to use foo-bar-1.2.3-SNAPSHOT.jar , you need to add the following line to module-info.java :

 module <name> { requires foo.bar; } 
+11
source share

Just in simple steps to use a third-party jar (e.g. log4j-api-2.9.1.jar below) in your module: -

  1. Run jar tool descriptor command

     jar --file=/path/to/your/jar/log4j-api-2.9.1.jar --describe-module 

    This will give you a conclusion similar to

    Module descriptor not found. Derived auto module .

    log4j.api @ 2.9.1 automatic

  2. In your module-info.java declare requires to the name of this module as: -

     module your.module { requires log4j.api; } 

    It.

+8
source share

It seems that I am missing something simple. I am trying to include tagsoup-1.2.jar in my Java 11 project. "Description-module" shows the expected result:

 > jar --file=D:/java11/libs/tagsoup-1.2.jar --describe-module No module descriptor found. Derived automatic module. tagsoup@1.2 automatic requires java.base mandated contains org.ccil.cowan.tagsoup contains org.ccil.cowan.tagsoup.jaxp main-class org.ccil.cowan.tagsoup.CommandLine 

So I expanded my module-info.java so that it looks like this:

 module util { requires java.desktop; requires tagsoup; } 

But the eclipse shows the following error: tagoup cannot be resolved for the module

What am I missing? Any help is much appreciated!

0
source share

All Articles