how do you make getResourceAsStream work while debugging Java in Eclipse?

A few notes:

First — as Chocos says, put it in the eclipse source dirs, not the binary dirs. Eclipse will clear the binary dirs when you “clean”, as well as clean up unmatched files. It will copy non-java source files to the binary dir.

This means that even though you drop the file in the binary dir, it may be deleted by eclipse…

Keep in mind that getResourceAsStream and getResource operate relative to the package of the code that calls them. For example:

package a.b.c;
public class Foo {
   ...
   getClass().getClassLoader().getResourceAsStream("fee.txt");
   ...
}

This will actually look for a/b/c/fee.txt; the package is pre-pended. This works well if you have the fee.txt in the same source dir as the Foo.java file, or if you have a separate set of resource dirs on the classpath with the same directory structure.

If you use

...getResourceAsStream("/fee.txt");

it will look for fee.txt directly on the classpath.

When you run from the command-line, where in the JAR is the resource file?
— Scott

Leave a Comment