I'm currently trying to load resources from my resources folder in Java. Sounds easy, well I thought so too, because I've already done it in other projects. I switched from eclipse to IntelliJ Idea a little while ago and haven't used resource loading since eclipse.
I looked at all kinds of tutorials and looked up tons of Q/A here, but none of them seem like they work for me (although they use the exact same code that I used previously).
My setting:
- Maven 3.5.0
- IntelliJ IDEA 2017.1.4
- Java 1.8.0_131
The structure of my code is the following: Project structure in IntelliJ
and my the resources section of my pom.xml looks like this:
<build>
<plugins>
<plugin>
<artifactId>
maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>
com.test.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>
.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>
make-assembly</id>
<phase>
package</phase>
<goals>
<goal>
single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>
src/main/resources</directory>
<targetPath>
${project.build.directory}/resources/</targetPath>
<includes>
<include>
id_rsa</include>
</includes>
</resource>
</resources>
</build>
I use this code to access my ssh key (I can't just use the default location for this, trust me):
public static void main(String[] args) {
Class c = Main.class;
URL url = c.getResource("id_rsa");
System.out.println(url.toString());
}
I suspect that something is wrong with where my resources folder is lying or what directory I marked as sources root....
P.S.: It didn't work using
URL url = c.getResource("/id_rsa");
URL url = c.getResource("resources/id_rsa");
URL url = c.getResource("/resources/id_rsa");
--------------EDIT---------------------
My question was not What is the difference between Class.getResource() and ClassLoader.getResource()?, I'm aware of the difference, tried them both and neither of them worked. My question was simply, if somebody can see some obvious fault in my project that hinders finding resources in general.
But looking through the answers enforce my suspicion that there's something wrong with how the project is structured. I hope somebody can help me ^^
Best regards!
---------EDIT #2--------------------- Debug-Info:
ClassLoader.getResource(String name) {
URL url;
if (parent != null) {
url = parent.getResource(name);<----null
} else {
url = getBootstrapResource(name);
}
if (url == null) {
url = findResource(name);<----also returns null
}
return url;
}
-------------------------Solution------------------------------
Turns out, it was simply the fact, that java puts a "file://" in front of the path and if you call this.getClass().getResource("/id_rsa").getFile() it puts an "/" in front of the path. Thus, it does not work for Windows, I had to call new File(this.getClass().getResource("/id_rsa").getFile()).getAbsolutePath() to get the full, correct absolute path to the file.
Thanks for the comments ;)
mvn clean install
it actually copies the requested file into a folder named resources if that's what you mean... – JosefRucksack Oct 12 '17 at 15:52