Search code examples
javajarmeta-inf

Can't locate resource file in JAR


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?


Solution

  • 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");