How do I load property files and images stored in the jar file?
Hi
So, if I have a structure like this:
/com.myfirm.myproject.SomeClass
/images/
/properties/
And I pack this structure into a jar file, how do I then access the images under "/images/" and the property files under "/properties/"? A link to a good tutorial on this would be perfect, or a description/hint/howto :)
[350 byte] By [
neigaarda] at [2007-11-26 17:07:12]

Try this in a SomeClass method. Note the initial "/".
URL url1= this.getClass().getResource("/images/duke.gif");
URL url2= this.getClass().getResource("/properties/config.properties");
//or
InputStream in1 = this.getClass().getResourceAsStream("/images/duke.gif");
InputStream in2= this.getClass().getResourceAsStream("/properties/config.properties");
Im having problems even loading direcly from the directory..? I have this structure:
src (source dir)
classes (classes dir)
lang/textRb_en_US.properties (the file I want to load)
Then I have this code:
Locale currentLocale = new Locale("en", "US");
ResourceBundle textRb = ResourceBundle.getBundle("/lang/textRb", currentLocale);
But it gives me a "java.util.MissingResourceException: Can't find bundle for base name /lang/textRb, locale en_US". I start my application from within Eclipse, and I have tried to move "lang/textRb_en_US.properties" into the classes dir, but same error.
Im confused as to there Java looks for ressources, and I need this to work no matter where I put my classes/jar. Could you help me out here?
Resource bundles are a little different -- I didn't realize you wanted to load one. Use use dot instead of /:
ResourceBundle textRb = ResourceBundle.getBundle("lang.textRb", currentLocale);
For details, read the API: [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader)]readBundle[/url].