Hi,

I have a problem with some resource files (i.e. .txt) which I have created in the project under let's say com/myapp/resources.

I have a class created: com.myapp.util.MyClass which reference a file.txt located in resources folder:

MyClass.class.getResourceAsStream("../resources/file.txt");

The project is created in eclipse. After I export the project as runnable jar I try to execute the jar and I get a null InputStream.

Could you guys please tell me what else is to be configured (something in the MANIFEST file probably... :| )

Thanks in advance!

All I can say at the moment is check your spelling. On windows when not packaged in a jar it will not be case sensitive, but once is packed into the jar it is case sensitive.

Edited 4 Years Ago by masijade: forgot "say"

Got the solution at last. The path to the file was incorrect.
It seems that the path has to begin with a '/'. So instead of

MyClass.class.getResourceAsStream("com/<path>/resources/file.txt");

I will have

MyClass.class.getResourceAsStream("/com/<path>/resources/file.txt");

Any specific reason you want to load resource relative to a class and not relative to a classloader using MyClass.class.getClassLoader().getResourceAsStream("my/pkg/file.txt") ?

Edited 4 Years Ago by ~s.o.s~: Inline code

Nope. It was just a scenario. But you are right. A resource should be referenced by root folder and not related to a class. It's more general.

Thanks!

This question has already been answered. Start a new discussion instead.