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
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.
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.
Putting my /resources/ folder into my /bin/ folder solved this issue for me.
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.
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.
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.