13

I cannot get getResourceAsStream to find a file. I have put the file in the top level dir, target dir, etc, etc and have tried it with a "/" in front as well. Everytime it returns null.

Any suggestions ? Thanks.

public class T {
 public static final void main(String[] args) {

  InputStream propertiesIS = T.class.getClassLoader().getResourceAsStream("test.txt");

  System.out.println("Break");
 }
}
tangens
  • 39,095
  • 19
  • 120
  • 139
Chris
  • 201
  • 1
  • 3
  • 5

6 Answers6

13

Put your file "test.txt" into the same directory where the java file of your class is (same package). Then use

T.class.getResourceAsStream( "test.txt" );

This works, because eclipse automatically copies the file as a resource to the classpath. When using the command line, you have to do this by hand.

tangens
  • 39,095
  • 19
  • 120
  • 139
  • THANK you ...please can you explain why it is not working with T.class.getClassLoader().getResourceAsStream.. – Chris Feb 04 '10 at 17:42
  • 4
    If you use `Class.getResourceAsStream( name )`, `name` is resolved internally. "test.txt" is transformed into "my/package/test.txt" and "/test.txt" is transformed into "test.txt". Then `ClassLoader.getResourceAsStream()` is called with the transformed name. Your code should work with both methods, but you have to check that your file "test.txt" stays in the right place (inside your classpath). With your call, you have to put "test.txt" inside your top level package directory. – tangens Feb 04 '10 at 19:09
12

You shouldn't need to add these files to the same directory to get it to work.

I was getting this symptom when I created a new Package and Source Folder to hold my junit tests. The tests would fail because getResourceAsStream was returning null.

Here's how to fix it:

Right-click the class (in my case, the new junit test class) in the Eclipse Project Explorer View

Build Path -> Configure Build Path -> Java Build Path -> Source Tab -> Add Folder

Select the folder that holds your files.

Ben L.
  • 121
  • 1
  • 4
  • If you already have the folder added to your build path, make sure you aren't excluding anything you're trying to use – MarkyDD Mar 14 '18 at 13:17
  • 1
    That was my issue, `src/main/resources` had an exclusion of `**` for some reason. Removing that solved it. – James Oct 30 '18 at 22:59
  • @James 5 years later and I had the same issue. I wonder where it's coming from because it suddenly appeared. My resources loaded fine a week ago. I suspect Maven. – user1803551 May 19 '23 at 01:20
4

Sometimes you need to tell eclipse explicitly what types of files to copy from the source folder to the distribution (classes) folder.

i have Eclipse SDK, Version: 3.7.1, Build id: M20110909-1335, Indigo and in this i did the following changes.

Project -> Properties -> Java Build Path -> Source (tab) -> Included (list item) -> Edit (button) to add */.txt to the existing */.java.

ksridhar
  • 189
  • 2
  • 9
0

Putting my /resources/ folder into my /bin/ folder solved this issue for me.

CamHart
  • 3,825
  • 7
  • 33
  • 69
0

Old question but I just had the same problem and none of the answers worked for me (or I didn't understand them).

In Eclipse, refresh the project directory to make Eclipse aware that a new file has been added to resources. For this, right-click on the project (topmost) directory in the package explorer view, then hit "Refresh". The same if you edit an existing resource file from outside eclipse: refresh to make eclipse aware of the edits.

dariober
  • 8,240
  • 3
  • 30
  • 47
0

Also make sure your file does not match any pattern of Preference>Java>Compiler>Building>OutputFolder>Filtered resources:.

For example, if you set *.txt in that field, you will not get test.txt to the build output.

chengdong
  • 3,139
  • 2
  • 14
  • 3