I built a small (hello world style) java 9 project using gradle and the jigsaw plugin.
For a junit test I tried to access some file as a resource, but classLoader.getResource(filename)
is not able to locate the file.
Usually you put the file as src/test/resources/foo.txt
and access it as /foo.txt
, but with Java 9 it looks like things changed and resources are (similar like classes) encapsulated in modules.
Where exactly do I have to put the resource file for gradle to package it correctly?
What is the right way to access that file afterwards in the test?
What is the documentation I forgot to read? :)
To be more precise I set up a small test project.
Use git clone https://github.com/michas2/java_9_resource_test.git
and do a gradle build
afterwards after changing to that directory. (I'm using gradle 4.6, but that should not be the problem.)
The structure looks like this:
.
├── build.gradle
└── src
├── main
│ └── java
│ ├── module-info.java
│ └── resourceTest
│ └── Main.java
└── test
├── java
│ └── resourceTest
│ └── MainTest.java
└── resources
└── resourceTest
└── test.txt
I try to access the resource using this.getClass().getResource("/resourceTest/test.txt")
, which unfortunately gives null
in return.