Emrakul Emrakul - 3 months ago 52
Java Question

getResourceAsStream returns null

I'm loading a text file from within a package in a compiled JAR of my Java project. The relevant directory structure is as follows:


The code being used to load the file is:

public class Lifepaths {
public static void execute() {

private Lifepaths() {}

//This is temporary; will eventually be called from outside
public static void main(String[] args) {execute();}

The print out will always print
, no matter what I use. I'm not sure why the above wouldn't work, so I've also tried:

  • "/src/initialization/Lifepaths.txt"

  • "initialization/Lifepaths.txt"

  • "Lifepaths.txt"

Neither of these work. I've read numerous questions so far on the topic, but none of them have been helpful - usually, they just say to load files using the root path, which I'm already doing. That, or just load the file from the current directory (just load
), which I've also tried. The file is being compiled into the JAR in the appropriate location with the appropriate name.

How do I solve this?


Lifepaths.class.getClass().getResourceAsStream(...) loads resources using system class loader, it obviously fails because it does not see your JARs

Lifepaths.class.getResourceAsStream(...) loads resources using the same class loader that loaded Lifepaths class and it should have access to resources in your JARs