noob here, I have a multi module maven project but I can't load a xml file under resources folder. structure of the project:
and the TestClass
source code is like the following snippet which is I stole from the internet:
public class TestClass {
public static void main(String[] args) throws IOException {
TestClass test = new TestClass();
InputStream is = test.getFileFromResourceAsStream("general-analyser-job.xml");
}
private InputStream getFileFromResourceAsStream(String fileName) {
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);
if (inputStream == null) {
throw new IllegalArgumentException("file not found! " + fileName);
} else {
return inputStream;
}
}
}
inputStream
is alwayse null!
problem solved, it was silly one: in pom.xml
of the module under build
tag resources get excluded like:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/*.*</exclude>
</excludes>
</resource>
</build>
just comment out <excludes>
and everything works
First you don't need to add
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
...
You can simply remove that part because it's the default (convention over configuration).
Furthermore you should put your test class TestClass.java
into src/test/java
plus a package name. You shouldn't put the TestClass
directory into src/main/java
which is the default package which shouldn't being used.
To access the resource you could go like this:
this.getClass().getResourceAsStream("/general...");
Based on your post I'm not sure if TestClass
is really meant as test or as test implementation for production code later on. If so you should move it into a real package and rename it to something more useful.
try to put your xml
file into src/test/resources
and then use code like below:
InputStream is = TestClass.class.getClassLoader().getResourceAsStream("general-analyser-job.xml");
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.