How to Use 3Rd Party Library in Java9 Module

How to use 3rd party library in Java9 module?

You can use your library as an automatic module. An automatic module is a module that doesn't have a module descriptor (i.e. module-info.class).

But what name do you need to specify to refer to an automatic module? The name of the automatic module is derived from the JAR name (unless this JAR contains an Automatic-Module-Name attribute). The full rule is quite long (see Javadoc for ModuleFinder.of), so for simplicity, you just have to drop the version from its name and then replace all non-alphanumeric characters with dots (.).

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;
}

Java 9 Modules: Can automatic Modules result in larger (full) projects?

I think you misunderstood how automatic modules work. Their critical property is that you can use existing, non-modular JARs, put them on the module path and have them appear as modules for compilation or at run time.

I guess automatic modules means that each module of a project that requires a 3rd party jar will have to package that jar inside it's own modular jar.

No, not at all. Quite the opposite, you reuse existing JARs.

Unable to derive module descriptor for auto generated module names in Java 9?

The solution to this seems to be:-

  • A way possible to uninterruptedly using the same artifact name with a new(different) module name could be by packaging META-INF/MANIFEST.MF of the artifact with an attribute Automatic-Module-Name which governs the name of the module to be used by the module descriptor when converted as an automatic module.

OR

  • Artifact owners can add module declarations using module-info.java to their JAR. (this could result in a slow bottom-up migration)

Since the module declaration defined in the specs as:

A module declaration introduces a module name that can be used in
other module declarations to express relationships between modules. A
module name consists of one or more Java identifiers (§3.8) separated
by "." tokens.


Intersetingly the declarations suggests -

In some cases, the Internet domain name may not be a valid package
name. Here are some suggested conventions for dealing with these
situations:

  • If the domain name contains a hyphen, or any other special character
    not allowed in an identifier (§3.8), convert it into an underscore.

  • If any of the resulting package name components are keywords (§3.9),
    append an underscore to them.

  • If any of the resulting package name components start with a digit, or
    any other character that is not allowed as an initial character of an
    identifier, have an underscore prefixed to the component.

But keep in mind as you do so that Underscore is a keyword in Java9

Sample Image

int _;  // is would throw an error on javac based out of JDK9
int _native; // works fine

Is it possible to use dependencies without module-info.class in a Java 9 module

Yes, it is possible. What you are looking for are automatic modules.

To create one you simply put a regular JAR into a folder that you mention on the module path (e.g. guava-19.0.jar in a folder libs - see my example project). This tells the JVM to create a module from it. This module will get a name based on the file name (in this case guava), will read all other modules, and export all of its packages.

You can then require it with the name it was given (e.g. require guava; - see here).



Related Topics



Leave a reply



Submit