How to access to resources in external resource folder in Java 9 modular project [duplicate]

江枫思渺然 提交于 2020-08-06 05:01:01

问题


When I have the following code that attempts accessing to a resource:

foo/Main.java

package foo;
import java.io.*;

public class Main{
    public static void main(String... args) throws IOException{
        try(BufferedReader in = new BufferedReader(new InputStreamReader(
                            Main.class.getClassLoader().getResourceAsStream("foo/res.txt")))){
            for(String line = in.readLine(); line!=null; line=in.readLine()){
                System.out.println("Line : " + line);
            }
            System.out.println("EOF");
        }
    }
}

compile it:

javac foo/Main.java -d build/classes/

and add some resources file, then I will get something like this:

foo/
|--Main.java
`--build
   |--classes
   |  `--foo
   |     `--Main.class
   `--resources
      `--foo
         `--res.txt

and after running by executing following command:

java -cp "build/classes:build/resources" foo.Main

I will get the expected output, like:

Line : This is a
Line : test!
EOF

But problem occurs when I make the project modular. I added simplest module-info.java like this:

module-info.java

module bar {
}

and compile it:

javac foo/Main.java module-info.java -d build/classes/

then I will get the following file structure:

module-info.java
foo/
|--Main.java
`--build
   |--classes
   |  |--module-info.class
   |  `--foo
   |     `--Main.class
   `--resources
      `--foo
         `--res.txt

But when I execute the class by typing command

java -p "build/classes/;build/resources/" -m bar/foo.Main

I get an error message claiming that resource was not found.

Exception in thread "main" java.lang.NullPointerException
        at java.base/java.io.Reader.<init>(Reader.java:167)
        at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
        at bar/foo.Main.main(Main.java:7)

How can I access to the resources from inside the module?

Edit: Note that this question specifically asks how to access resources in an 'exploded' module (a module that has not been packaged as a JAR file). The key detail here is that in an exploded module, the resources folder is not automatically seen as part of the module unless specifically patched in on the command line as per @Alan Bateman's comment.

This makes the question not a duplicate of "Java 9 jigsaw - accessing resource files from external modules". That question involves granting access to resource files from one non-exploded module to another non-exploded module.

This question is also not a duplicate of "How to work with resources in Java 9 modules". That question involves a situation in which one "main" module is unable to access test-related resources placed within it via "--add-patch", due to the fact that the test-related resources folders are not being included by Gradle.

This question is also not a duplicate of "Java 9 Module system Resources files location". That question involves how to cause a module to access a resources folder which is outside of the module's own source tree.

The existing question which this question is probably closest to being a duplicate of is probably "Where do resource files go in a Gradle project that builds a Java 9 module?", which covers a couple of related scenarios in detail and is well worth reading if you are having problems with this issue.

来源:https://stackoverflow.com/questions/53018804/how-to-access-to-resources-in-external-resource-folder-in-java-9-modular-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!