Compilation issue with requires static

Jan Lahoda jan.lahoda at oracle.com
Fri Jan 21 18:25:33 UTC 2022


Hi Simone,


The full error is:

.../MainTest.java:12: error: cannot access JSON
         JSON json = new JSONService().json;
                                      ^
   class file for org.eclipse.jetty.util.ajax.JSON not found
1 error


The JSON class is available to the 'compiler.bug.app' module, and can be 
used there (there is no error for the variable's type, for example). But 
JSONService is from module 'compiler.bug.service', and its return type 
is JSON, and JSON is not available in the context of the 
'compiler.bug.service' module, leading to the error.


So I don't currently see a bug in javac for this.


In principle, one could specify '--add-reads 
compiler.bug.service=ALL-UNNAMED' (then the JSON class from the unnamed 
module would be available to 'compiler.bug.service'), but it is not 
clear to me why the dependent module is not simply put on the module path.


Jan


On 21. 01. 22 11:46, Simone Bordet wrote:
> Hi,
>
> 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?
>
> Thanks!
>


More information about the compiler-dev mailing list