views:

25

answers:

2

Hi Guys,

I try to load a property-file that is here

<project>/resource/text/translation_en.properties

With the following code (in a static context, no Objects are initialized from me right now):

<This_Classes_name>.class.getResourceAsStream("/resource/text/translation_en.properties");

Why do I load it this way? First I use the class Object because getResourceAsStream() is not static and I learned through googling that I should use this method to load my property-file. Then I use a / that Java does not look relative to my Main.java, but the Project. Though, I still get null using that.

How can this not work, if there is a file in that exact place? What does Java do here?

A: 

getResourceAsStream is used, because it can read files from the whole classpath (including files in JARs which are in the classpath)

I think the method takes the filename as an argument without any paths, in your example the classloader would look for a file called "/resource/text/translation_en.properties" not interpreting the first part as a path. try loading like this:

this.getClass.getClassLoader().getRsourceAsStream("translation_en.properties");

Additionally youll have to make sure that the file is actually in the classpath. With maven and eclipse there should be a folder "target" which has all your generated classes in it. This folder gets added to the classpath when the project is run in eclipse.

Hope that helped....

smeg4brains
A: 

The resources need to be in the Classpath. One option is to copy the whole /resource folder int your source folder. One other option is to create a new source folder containing only the resources and move the /resource folder there. Don not make the /resource folder a source folder unless you want to use getResourceAsStream("/text/translation_en.properties");

Tassos Bassoukos