Compilation issue with requires static

Alex Buckley alex.buckley at oracle.com
Fri Jan 21 19:12:23 UTC 2022


On 1/21/2022 2:46 AM, Simone Bordet wrote:
> the issue is described in detail here:
> https://issues.apache.org/jira/browse/MCOMPILER-481.
> 
> To summarize, we have module A with: requires static X.
> Then we have module B with: requires A.
> Module B has test classes (that are patched into module B), and to
> compile the test classes X is added to the class-path, and --add-reads
> B=ALL-UNNAMED is also added.
> 
> My understanding is that a test class in module B should be able to
> read classes from module X, due to the --add-reads directive.
> 
> However, the compiler (triggered by Maven, but you can see the full
> command line in the issue linked above) reports:
> 
> [ERROR] /home/simon/tmp/compiler-bug/app/src/test/java/org/test/app/MainTest.java:[12,38]
> cannot access org.eclipse.jetty.util.ajax.JSON
> [ERROR]   class file for org.eclipse.jetty.util.ajax.JSON not found
> 
> where MainTest is a test class in module B, and JSON is a class in module X.
> 
> The workaround is to put the jar of module X in the module-path, but
> that seems unnecessary given the --add-reads directive.
> 
> Is the error expected behavior, or a javac bug?

I agree with Jan that javac is unlikely to be at fault here.

The intent of module A is that module X be on the modulepath at 
compile-time. I realize you are not compiling module A, but rather 
module B (or at least the test classes patched into B), but it would 
still be simpler to put X on the modulepath instead of messing around 
with the classpath.

Then, you should do `--add-modules X --add-reads B=X` to ensure that X 
is, first, resolved (A's `requires static X;` is moot as you're not 
compiling/resolving A), and second, readable by B.

Alex


More information about the compiler-dev mailing list