Skip to content

Accessing resources in subdirectoriers via java module API fails in native-image, when resource path starts with / #5219

@Rottinator

Description

@Rottinator

Hello there!

Describe the issue
When accessing a resource via the java module API for example via java.lang.Module#getResourceAsStream and the resource is located in a subdirectory and the resource name starts with an /, the API returns NULL instead of the InputStream for the resource.
This happens only in native-image. In JVM-Mode everything works fine.

For example:

public class Application {
    public static void main(String[] args) {
        InputStream resourceStream = Application.class.getModule().getResourceAsStream("/package/resource.txt");	
        assert resourceStream != null : "Resource is null!";
    }
}

will work in JVM-Mode, but not in native-image.
Removing the / at the beginning fixes the problem, but it should also work with the / in the path.

Steps to reproduce the issue

  1. Clone repository at https://github.com/Rottinator/graalvm-module-sample.
  2. Build the project via docker build -t graalvm-module-sample .
  3. Run the JVM-Version via: docker run --rm graalvm-module-sample java --module-path /code/target/graalvm-module-sample-1.0-SNAPSHOT.jar --module graalvmmodulesample/de.example.graalvmmodulesample.Application
  4. Run the native-image-Version via: docker run --rm graalvm-module-sample

Both versions should have the same output, but the native-image version fails, while loading the resource via module API.

Describe GraalVM and your environment:

  • Tried it with 22.2.0 Java 17 and also with latest dev build
  • OS: Happens on Windows and Linux
  • Architecture: ** AMD64**

This issue was already discussed in the native-image slack channel at https://graalvm.slack.com/archives/CN9KSFB40/p1664480047678039 and was identified as bug from @olpaw

Best regards
Christoph

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions