简体   繁体   中英

The method getResourceAsStream returns null on ubuntu

Recently I came accross very strange behaviour of getResourceAsStream method.

I have an application that I developed on Windows. This app is a desktop application based on OSGI framework. I created a bundle, set up needed packages and added some data files to those packages so that they are contained within the jar file.

I load the data using that:

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

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

I then moved the development to ubuntu 12.04. An to my surprise the method mentioned above always returns null. The data is where it should be. All the settings look OK. No absolute paths in any config files.

I tried many different things. I altered the path to:

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

I tried not to contain the package root but insert a path that is relative to the class location (lets assume that my class is: pl.com.myapp.MyClass):

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

I also tried:

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

But nothing worked :(

But when I create a simple Java application everything works smoothly and the method returns the proper stream.

Did anyone come accross such problems ?

I use eclipse-juno on ubuntu 12.04.

请注意区分大小写,因为与Windows相比,Linux路径区分大小写。

Just a guess, but is it possible that this.getClass() is not the class you think it is? For example if somebody subclasses you then this.getClass() will return the subclass rather than your baseclass, which may be in another bundle and therefore won't have visibility of the resource.

When doing these kind of resource lookups you should always use the literal class name, eg: MyClass.class.getResourceAsStream() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM