2

I am trying to access a file from my project's directory, let's call it

src/main/project/foo/bar.txt

In class MyClass

But when I do:

InputStream is = MyClass.class.getResourceAsStream("bar.txt");

And do anything with the InputStream, e.g.

is.toString();

I get a NullPointerException

I have also tried:

MyClass.class.getResourceAsStream("/bar.txt");

and

MyClass.class.getResourceAsStream("./bar.txt");

Is there something extra I need to do as the file is not in the same package as the class in which I'm calling it? This application is deployed on TomCat, so absolute references to the filepath are impractical.

EDIT: To the person who flagged it as a duplicate of nullpointer, I know what a nullpointer is, obviously it can't find the resource. As to why that is the case, I need help figuring out.

mstorkson
  • 1,130
  • 1
  • 10
  • 26
  • 3
    What makes you think `src/main/project/foo/bar.txt` get somehow "bundled" into your classpath? –  Jan 23 '17 at 19:01
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Code-Apprentice Jan 23 '17 at 19:01
  • How do you run your app? What is your classpath? – Code-Apprentice Jan 23 '17 at 19:02
  • The duplicate which I flagged doesn't just explain what a NPE is. It also gives tips about how to figure out the cause. After reading it, you will have a better idea of what you can do to debug your problem. You will also know what information is missing from this question that we need to help you. – Code-Apprentice Jan 23 '17 at 19:03
  • If you're using some ide say eclipse go to configure build path and put the folder location to that and then it should work. Just calling resourceAsStream is not going to recursively look through each and every folder to search for the file as you are expecting. – shashwatZing Jan 23 '17 at 19:04
  • @shashwatZing I'm using netbeans, but I'm deploying to tomcat. I've tried a ton of iterations of all the various paths, but the path seems to be different when the application is actually running on Tomcat. – mstorkson Jan 23 '17 at 19:05
  • Perhaps [this](http://stackoverflow.com/questions/16570523/getresourceasstream-returns-null) would be a better dupe. Let us know what you learn from it. – Code-Apprentice Jan 23 '17 at 19:05
  • @Code-Apprentice that's at least similar to what I'm experiencing, but none of the answers are helpful. Basically I want to have a folder called "fonts" sitting somewhere in the project, so that I can load fonts from it. I have a folder that contains these things, in the project. But I'm having difficulty figuring out how to make those contents available to use. – mstorkson Jan 23 '17 at 19:09
  • 1
    Check [this](https://punekaramit.wordpress.com/2010/03/11/decoupling-executable-jar-and-configuration-files/) to see if it helps. – Amit Jan 23 '17 at 19:10
  • @mstorkson You should definitely do some research about how `getResourceAsStream()` locates the given file. – Code-Apprentice Jan 23 '17 at 19:13
  • @Amit thank you, that's exactly what I was looking for. Thanks for giving me that link – mstorkson Jan 23 '17 at 19:14
  • `MyClass.class.getResourceAsStream("bar.txt")` looks for a bar.txt *in the same directory inside the .jar or .war file* where MyClass.class resides. Instead of guessing, verify that bar.txt is where it’s supposed to be in your archive. – VGR Jan 23 '17 at 19:24

1 Answers1

2

if your class is at src/main/project/foo/MyClass.java MyClass.class.getResourceAsStream("bar.txt"); should be fine. if your resource is in a different folder within the project than the class use:

MyClass.class.getClassLoader().getResource("main/project/foo/bar.txt");

in general:

MyClass.class.getClassLoader().getResource("package/<subpackage/>filename");

To my knowledge this works with any class anywhere within the same project. MyClass.class.getResourceAsStream(); operates relative to the Location of myClass and you can't change that.

Poohl
  • 1,932
  • 1
  • 9
  • 22