Can't locate resource file in JAR

awesomemypro :

I have a class in my Java application, which reads a resource folder which is located in src/main/resources. So, in that class, I am specifying the resource folder as:

public static final String RES_LOCATION = "./src/main/resources";

The program runs fine. But when I make a jar and use that as a dependency in another project, that above class fails as it cannot find the resource folder.

Any solution?

Jigar Joshi :

You need to read particular file from that directory as a classpath resource instead of java.io.File

getClass().getResourceAsStream("relative/class/path/to/resource");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140832&siteId=1