2

In JDKs 9 and 10, there used to be a few modules such as java.xml.bind, containing Java EE classes. They were marked as deprecated and to be removed with the advent of JDK 9 and finally removed in 11 (see JEP 320). In a product I am contributing to, there used to be tests for the javac compiler option --add-modules, adding those modules as root modules. Those tests have been deactivated for JDK 11+. Instead of removing them, I would like to reactivate them, if there are any other JDK modules which are also non-root by default. The tests could then just use those modules instead.

I know I can just test --add-modules with my own modules, but then I have to specify them on the module path. The test case that an extra module path is not necessary for JDK modules added via --add-modules is also interesting, if any JDK 11+ modules still exist to be tested against. I am not talking about non-exported packages, but really about non-root JDK modules.

So, according to the information in this answer, I am actually looking for non-java.* modules among the system modules which do not export at least one package without qualification. In that case, those modules should not be root, and they would be eligible for my test case.


Update: What I am looking for is an equivalent for this in JDK 9:

import javax.xml.bind.JAXBContext;

public class UsesJAXB {
  JAXBContext context;
}
xx> "C:\Program Files\Java\jdk-9.0.4\bin"\javac UsesJAXB.java
UsesJAXB.java:1: error: package javax.xml.bind is not visible
import javax.xml.bind.JAXBContext;
                ^
  (package javax.xml.bind is declared in module java.xml.bind, which is not in the module graph)
1 error

xx> "C:\Program Files\Java\jdk-9.0.4\bin"\javac --add-modules java.xml.bind UsesJAXB.java

See? With --add-modules it builds, without it does not.

I am looking for modules (if any) in JDK 11-18, which would yield the same result when importing their classes in a simple program, i.e. require them to be explicitly added via --add-modules for compilation (not talking about runtime).

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • 1
    What exactly are these tests testing? – Holger Mar 31 '22 at 15:54
  • I am an AspectJ committer, but not a JPMS expert. Mostly, I ignore the existence of Java modules, if I can. Anyway, the AspectJ compiler is a fork of the Eclipse Java compiler. While fixing a JPMS-related problem, I found a bunch of other module tests deactivated since JDK 11, because they used the removed Java EE classes which used to be part of the JDK before. I was just looking for other JDK classes which would allow me to test the (corner) case `--add-modules` without a simultaneous `--add-exports`. I simply do not like ignored tests. – kriegaex Apr 01 '22 at 10:11

1 Answers1

1

You can list all modules of the jdk with:

java --list-modules

Then you can print the module descriptors with:

java --describe-module a.module.name

After filterting these outputs in a little script, here are the modules of my JDK 17 than could qualify:

jdk.charsets
jdk.crypto.cryptoki
jdk.crypto.ec
jdk.editpad
jdk.internal.vm.compiler
jdk.internal.vm.compiler.management
jdk.jcmd
jdk.jdwp.agent
jdk.jlink
jdk.jpackage
jdk.localedata
jdk.zipfs

jdk.charsets, for instance, is a module providing a service.


Update after question update

So you are looking for a module that exports a package but is not in the default module graph when compiling a class in the unnamed module. According to the JEP, only java.* modules can qualify.

When I'm looking for modules required, either directly or indirectly, by java.se (which is a root module when it exists), I see all java.* modules of the JDK, but java.smartcardio. And due to some unknown magic, java.smartcardio is also in the default graph : I tried to compile a class importing one of its class and it works without --add-modules.

So I think you are down to using a non-java module exporting no package (like jdk.charsets), importing one of its class (like sun.nio.cs.ext.ExtendedCharsets) and either:

  • add --add-exports in addition to --add-modules when compiling your test class so that javac succeeds.
  • or parse the error message of javac and distinguish between "not in the module graph" and "not exported".
  • Would you mind describing what you filtered for? then I can create a similar scipt for myself and also understand how you got those results. – kriegaex Mar 29 '22 at 10:21
  • One more thing, I tried using classes from some of those modules in a plain Java program. The program compiles just fine without `--add-modules` on the _javac_ command line, i.e. those modules do not seem to qualify, i.e. they seem to be root modules. I only tried two or three, though. This answer does not seem to be correct. – kriegaex Mar 29 '22 at 10:40
  • I filtered out all modules exporting a package, that is which descriptors does not contain a line starting with `exports`. – Hervé Guillemet Mar 29 '22 at 11:07
  • I tried to compile a source file with `import sun.nio.cs.ext.ExtendedCharsets`, that is the class in `jdk.charsets` that implements the `CharsetProvider` service. Without the `--add-modules` I get error `package sun.nio.cs.ext is declared in module jdk.charsets, which is not in the module graph` because the module is not a root module. With the `--add-modules` the error is different: `package sun.nio.cs.ext is declared in module jdk.charsets, which does not export it`. – Hervé Guillemet Mar 29 '22 at 11:15
  • I updated my question, describing the use case in more detail. I also just saw your new comment and am going to try with `ExtendedCharsets`. – kriegaex Mar 29 '22 at 11:17
  • `ExtendedCharsets` is a step forward, but I needed an export, too: `javac --add-modules jdk.charsets --add-exports jdk.charsets/sun.nio.cs.ext=ALL-UNNAMED`. It would be great to also find a class which is exported, even though the module is not in the root module graph. – kriegaex Mar 29 '22 at 11:23
  • I just happened to notice your edit in the answer by chance, because on SO there are no notifications for updated answers or questions. In those cases, an additional comment is helpful. Yesterday I decided to do exactly what you recommended and what I had mentioned in my previous comment: use `--add-exports` in addition to `--add-modules`, because probably since the removal of those Java EE classes there are no more JDK classes which fulfill my criterion. Probably that was part (or side effect) of the whole clean-up action. – kriegaex Mar 31 '22 at 08:00
  • 1
    Well, there are [Incubator Modules](http://openjdk.java.net/jeps/11) which are not available to non-modular software unless `--add-modules` has been specified. In the current JDK, there are `jdk.incubator.vector` and `jdk.incubator.foreign`. But it would be pointless to write test cases based on them, when they are *by definition* about to disappear in a future release. – Holger Apr 01 '22 at 10:22