7

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?

Renato
  • 12,940
  • 3
  • 54
  • 85

1 Answers1

3

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
lojoe
  • 521
  • 1
  • 3
  • 13
  • `ALL-UNNAMED` could just be `my.mod` here I guess. I [had answered](https://stackoverflow.com/a/46891860/1746118) a related question that does the inverse in terms of resources, where I've linked the [encapsulation strategy](https://docs.oracle.com/javase/9/docs/api/java/lang/Module.html#getResourceAsStream-java.lang.String-) of resources. You can make use of that as well. Would add value to the answer in terms of the `dot.separated.path.to.resources`. – Naman Oct 23 '17 at 15:32