Giancarlo Giancarlo - 7 months ago 28
Java Question

open resource with relative path in java

In my java app I need to get some files and directories.

This is the program structure:

./main.java
./package1/guiclass.java
./package1/resources/resourcesloader.java
./package1/resources/repository/modules/ -> this is the dir I need to get
./package1/resources/repository/SSL-Key/cert.jks -> this is the file I need to get


guiclass
loads the resourcesloader class which will load my resources (directory and file).

About the file, I tried

resourcesloader.class.getClass().getResource("repository/SSL-Key/cert.jks").toString()


in order to get the real path, but this way does not work...

About the dir I have no idea how to do...

Suggestions?
Thanks.

Answer

Supply the path relative to the classloader, not the class you're getting the loader from. For instance:

resourcesloader.class.getClassLoader().getResource("package1/resources/repository/SSL-Key/cert.jks").toString();