Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let an automatic module find its own resources in Java 9?

Tags:

java

java-9

I am trying to get my application to run with Java 9, but unfortunately one of the plain jar dependencies, when it tries to load a resource using classLoader.getResource(name), gets a null instead.

This, of course, works in Java 8.

I declared a dependency on the module in question using the module file, referring to the name of the module by its jar name (awful), and added the jar as-is (no customization for Java 9) using the --module-path option.

Here is my approximate module file declaration:

module my.mod {
    requires ivy; // the file is called ivy-2.4.0.jar
}

And I run with this command:

java --module-path my-mod.jar:ivy-2.4.0.jar -m my.mod

When I run the application, it works fine if the library doesn't try to load that resource... but if it does, it gets a NullPointerException at the line it tries to use the resource.

I can see the resource is present in the correct path in the jar file.

I've tried running my application both as a jar (shown above) and just with the class files:

java --module-path modules-dir:ivy-2.4.0.jar -m my.module/my.Main

In the latter case, the error is different, but related: Ivy can't even find a properties file it tries to load from its own jar!

Is there any workaround, is this a known bug, or am I doing something wrong?

like image 337
Renato Avatar asked Apr 23 '17 16:04

Renato


People also ask

Which is correct about module system in Java 9?

Java Module System is a major change in Java 9 version. Java added this feature to collect Java packages and code into a single unit called module. In earlier versions of Java, there was no concept of module to create modular Java applications, that why size of application increased and difficult to move around.

What is the base module of all the modules in Java 9 is?

java. base is known as the "mother of Java 9 modules." In the following image, you can see the modular aspects of the system and can probably make the leap to understand how a modularized JDK means that you can also modularize your own applications. This is only a few of the 98 platform modules.

Which module is available to your named module without needing a Requires directive in Java?

xml module, code in modules that read java. desktop becomes dependent on java. xml . Without the requires transitive directive in java.

What is the difference between module path and class path?

The classpath is a list of the class libraries that are needed by the JVM and other Java applications to run your program. Similarly, the modulepath is a corresponding list of Java modules needed by your program.


1 Answers

Try to add an 'opens' declaration to the automatic module 'ivy'. That allows access to it's resources:

java --add-opens ivy/<dot.separated.path.to.resources>=ALL-UNNAMED --module-path my-mod.jar:ivy-2.4.0.jar -m my.mod
like image 88
lojoe Avatar answered Sep 30 '22 11:09

lojoe