JavaCompiler called from JPMS module

Alan Bateman Alan.Bateman at oracle.com
Sat May 16 17:23:49 UTC 2020


On 16/05/2020 17:53, Alex Sviridov wrote:
> :
>   
>   
> When H2 is on classpath I don't have any problems - trigger is compiled. However, when H2 is on
> module path (H2 has in manifest Automatic-Module-Name: com.h2database) on some of child layers
> (not boot layer) I get the following `output`:
>   
>      /org/h2/dynamic/trigger/SOME_TRIGGER.java:6: error: package org.h2.api does not exist
>          public static org.h2.api.Trigger create() {
>                                  ^
>      1 error
>   
> As I understand the problem is with classloader and JavaCompiler. Could anyone say how it can be fixed?
>
You can't compile against modules in module layers. Instead you'll need 
to specify the module path where the compiler can find the JAR file with 
module com.h2database. This snippet might help:

         Path src = ...       // path to the source code for SOME_TRIGGER
         Path dir = ...       // directory that contains com.h2database
         Path output = ... // output directory

         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
         StandardJavaFileManager fm = 
compiler.getStandardFileManager(null, null, null);
         fm.setLocationFromPaths(StandardLocation.MODULE_PATH, 
List.of(dir));
         fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, 
List.of(output));
         Iterable<? extends JavaFileObject> paths = 
fm.getJavaFileObjects(src);
         JavaCompiler.CompilationTask task = compiler.getTask(null, fm, 
null, null, null, paths);
         task.addModules(List.of("com.h2database"));
         task.call();

The call to addModules is needed because SOME_TRIGGER isn't being 
compiled as a module so there is nobody to require com.h2database.

-Alan



More information about the jigsaw-dev mailing list