Eclipse getResourceAsStream returns null

I can not get getResourceAsStream to find the file. I put the file in the top level directory, destination directory, etc. Etc., And tried it with a "/" in front. Each time it returns null.

Any suggestions? Thank.

public class T { public static final void main(String[] args) { InputStream propertiesIS = T.class.getClassLoader().getResourceAsStream("test.txt"); System.out.println("Break"); } } 
+12
null eclipse resources
03 Feb '10 at 21:07
source share
6 answers

Put your test.txt file in the same directory as the java file of your class (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 must do this manually.

+12
03 Feb '10 at 21:14
source share

You do not need to add these files to the same directory to make it work.

I got this symptom when I created a new Package and Source folder for running junit tests. Tests fail because getResourceAsStream returns null.

Here's how to fix it:

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

Build Path → Configure Build Path → Java Build Path → Source Tab → Add Folder

Select the folder where your files are stored.

+9
Feb 28 '12 at 21:41
source share

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

I have the Eclipse SDK, version: 3.7.1, Build id: M20110909-1335, Indigo and in this I made the following changes.

Project → Properties → Java Build Path → Source (tab) → Enabled (list item) → Change (button) to add * /. txt into an existing * /. java.

+3
Dec 01 '11 at 17:52
source share

Also make sure your file does not match the pattern Preferences> Java> Compiler> String> Output> Filtered Resources:.

For example, if you install *. txt in this field, you will not get test.txt to output the assembly.

0
Apr 24 2018-11-11T00:
source share

Putting my / resources / folder in the / bin / folder solved this problem for me.

0
Dec 27 '13 at 11:20
source share

Old question, but I had the same problem, and none of the answers worked for me (or I did not understand them).

In Eclipse, update the project directory so that Eclipse knows that a new file has been added to resources. To do this, right-click on the project directory (the topmost one) in the package explorer view, and then click Refresh. Same thing if you are editing an existing resource file from external eclipse: update to make eclipse aware of the changes.

0
Mar 11 '17 at 10:24
source share



All Articles