Class.getResource() returns null in my Eclipse Application? Can't configure classpath?

Please take a step back. Your concrete problem is that the resource returns null, right? Are you sure that its path is right? As you have, it's relative to the package of the current class. Shouldn't the path start with / to be relative to the package root?

URL resource = getClass().getResource("/rsc/my_resource_file.txt");
// ...

Alternatively, you can also use the context class loader, it's always relative to the classpath (package) root:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL resource = loader.getResource("rsc/my_resource_file.txt");
// ...

At least, the Eclipse launcher is not to blame here.


Right click on the project and follow the properties.


Put the file in the top level directory in your source tree. This is often called "src". Then, when you build your project the file will be copied into your class directory (name varies). Finally, post build the file will be in your classpath (within the eclipse environment).

Class someClassObject = BlammyClassName.class;
someClassObject.getResource("my_resource_file.txt");

will return a URL to your resource.

someClassObject.getResourceAsStream("my_resource_file.txt");

will return a stream.

Edit: changed such that it does not reference Class methods statically.