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!

Recommended Answers

All 6 Replies

Did you check that the file is actually in the created jar?

Yes. It's there.

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.

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") ?

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.