Java – Why the Java Auto Module does not include an export section

Why the Java Auto Module does not include an export section… here is a solution to the problem.

Why the Java Auto Module does not include an export section

There is a mechanism to convert a regular jar to automatic module from start, By simply putting it on the modulepath instead of the classpath. Automatic modules have the following properties:

An automatic module is a named module that is defined implicitly,
since it does not have a module declaration. Every package in an
automatic module is, therefore, considered to be exported even if it
might actually be intended only for internal use.

So all packages in the automatic module are available. However, if we check the automatic module with the —describe-module directive, there is no exports part in the output.

For example, an automatic module main

jar --file main.jar --describe-module
com.foo jar:file:///.../code/module/main.jar/!module-info.class
requires java.base mandated
contains com.foo

If the package com.foo is considered exported anyway, why isn’t there an exports section? I find this a bit confusing: —describe-module indicates that there is no export package, but at the same time, main is an automatic module, so everything is implicitly exported.

Solution

I believe that the command-line option —describe-module in the jar tool just describes the contents of the jar file, if it is an explicit module sharing the full descriptor, whereas for automatic modules, only the name is described.

Reference the command jar --help

Print the module descriptor, or automatic module name


On the other hand, if you try Using jdeps for generating module-info.java In an automated module for a given jar file, you’ll notice that such package exports actually exist in the module declaration. For example:

jdeps -verbose:class --generate-module-info .. /Desktop  ~/.m2/repository/org/apache/commons/commons-lang3/3.8.1/commons-lang3-3.8.1.jar

Write .../Desktop/org.apache.commons.lang3/module-info.java below

module org.apache.commons.lang3 {
    requires transitive java.desktop;

exports org.apache.commons.lang3;
    exports org.apache.commons.lang3.arch;
    exports org.apache.commons.lang3.builder;
    exports org.apache.commons.lang3.concurrent;
    exports org.apache.commons.lang3.event;
    exports org.apache.commons.lang3.exception;
    exports org.apache.commons.lang3.math;
    exports org.apache.commons.lang3.mutable;
    exports org.apache.commons.lang3.reflect;
    exports org.apache.commons.lang3.text;
    exports org.apache.commons.lang3.text.translate;
    exports org.apache.commons.lang3.time;
    exports org.apache.commons.lang3.tuple;
}

Related Problems and Solutions