GetResourceAsStream method returns null on ubuntu

I recently met very strange behavior of the getResourceAsStream method.

I have an application that I developed on Windows. This application is an OSGI-based desktop application. I created the package, installed the necessary packages, and added some data files to these packages so that they are contained in the jar file.

I am loading data using this:

this.getClass().getResourceAsStream("pl/com/myapp/resource.dat"); 

It worked great both in eclipse and when I deployed my application and ran it on a system outside of the IDE.

Then I transferred the development to ubuntu 12.04. To my surprise, the method mentioned above always returns null. Data is where it should be. All settings look OK. There are no absolute paths in any configuration files.

I have tried many different things. I changed the path to:

 this.getClass().getResourceAsStream("/pl/com/myapp/resource.dat"); 

I tried not to contain the package root, but insert the path related to the location of the class (let's say my class is: pl.com.myapp.MyClass):

 this.getClass().getResourceAsStream("resource.dat"); 

I also tried:

 this.getClass().getResourceAsStream("./resource.dat"); 

But nothing happened: (

But when I create a simple Java application, everything works smoothly, and the method returns the correct thread.

Has anyone encountered such problems?

I am using eclipse-juno on ubuntu 12.04.

+7
source share
2 answers

Just guessing, but is it possible that this.getClass() is not the class that you think so? For example, if someone subclasses you, this.getClass() will return a subclass, not your base class, which may be in a different set and, therefore, will not have the visibility of the resource.

When performing such a resource search, you should always use the literal class name, for example: MyClass.class.getResourceAsStream() .

+2
source

Be careful about case sensitivity, as Linux paths are case sensitive compared to Windows.

+3
source

All Articles