Lancelot
Lancelot

Reputation: 2427

classloader.getSystemResourceAsStream returns null

I'm trying to load a properties file without using the actual path of the file. I've already done that on some other simple apps using:

InputStream inputStream = ClassLoader.getSystemResourceAsStream(PROPERTIES_FILE);
props.load(inputStream);

But this time it doesn't work. The inputStream is null for some reason. PROPERTIES_FILE is a constant defined as "app.properties". I tried to remove the .properties extension and got the same results.

Any ideas?

Thanks.

Upvotes: 7

Views: 17318

Answers (3)

Gogogo
Gogogo

Reputation: 1

Got the same problem.

Reason: I renamed DAO package to dao. While exploding the artifact, directory DAO not get overwritten.

So I got dao in project internals and DAO in filesystem :facepalm:

Upvotes: 0

jconlin
jconlin

Reputation: 3866

The PROPERTIES_FILE constant should include the package as well as the properties file (e.g. "com/some/library/file.properties".

    final static String PROPS_FILE = "/com/some/library/file.props";
                     //The preceding  "/" is dependendant on wheterh 
                     //you are going to be giving a relative or absolute location
    InputStream is = YourCurrentClass.class.getResourceAsStream(PROPS_FILE);

Upvotes: 13

erickson
erickson

Reputation: 269637

When getSystemResourceAsStream returns null, it means the resource was not found. Make sure the requested resource really is on the classpath at the specified location.

Upvotes: -1

Related Questions