Issue
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!
Solution
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.
Answered By - khmarbaise