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]
# 1
You need to get them as a resource.Google for something like "java jar resource"
zadoka at 2007-7-8 23:35:02 > top of Java-index,Java Essentials,Java Programming...
# 2

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");

DrLaszloJamfa at 2007-7-8 23:35:02 > top of Java-index,Java Essentials,Java Programming...
# 3
> A link to a good tutorial on this would be perfect,[url http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html#getresource]How to Use Icons[/url]
camickra at 2007-7-8 23:35:02 > top of Java-index,Java Essentials,Java Programming...
# 4

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?

neigaarda at 2007-7-8 23:35:02 > top of Java-index,Java Essentials,Java Programming...
# 5

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].

DrLaszloJamfa at 2007-7-8 23:35:02 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks! Well as it is a file, I thought it was the same :) But now I know that resource bundles are handled differently, thanks!/S鴕en
neigaarda at 2007-7-8 23:35:02 > top of Java-index,Java Essentials,Java Programming...