简体   繁体   中英

Can not load resource from package

I have a problem which seems to be very basic and simple but I am not able to solve it, even though I made investigations on the Internet. Since I believe that I ran into a corner and being not able to find my way out, I will state my very simple question here.

Given: A JDK 11 project containing one module called LoadResource .

Project structure

src
└── main
    ├── java
    │   ├── module-info.java
    │   └── com
    │       └── example
    │           └── LoadResource.java
    └── resources
        └── com
            └── example
                └── Foo.txt

module-info.java

module {
  exports example.com;
}

LoadResource.java

package com.example;

public class LoadResource {
    public static void main(String... args) {
        new LoadResource().run();
    }

    private void run() {
        System.out.println("TEST");

        final var resource = getClass().getResource("Foo.txt");
        System.out.println(resource);
    }
}

If I run this getClass().getResource("Foo.text"); returns null .

I know that's a very basic question, but: Why is that? To me, that looks like a rather simple programming task. What do I miss here?

我可以通过在IntelliJ --patch-module LoadResource=build/resources/main为VM参数来解决我的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM