views:

157

answers:

2

Hi All,

In order to internationalise my code, I use a property file called MessagesBundle_en_UK.properties which is located in a resource folder called config so that I the following structure:

+---src
      \---com
            \---proj
                   \---messages
                              Messages.java  
+---config
         \---languages
                    MessagesBundle_en_UK.properties

Messages.java has:

    currentLocale = new Locale("en", "UK");
    messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);

and I have added config/languages at the beginning of the classpath and it works fine when I run the program with javac/java from the command line.


My problem is that it doesn't work when I run the jar and I get this MissingResourceException error.

although the manifest does include config/languages:

    Class-Path: 
      ./config/languages 
      ./libs/xxxx.jar
      ./libs/...
      ...

I'm bit confused...

Thanks for the help David

+3  A: 

You should reference the location of your ResourceBundle relative to the root as if it were a member of a package:

messages = ResourceBundle.getBundle("config.lagnuages.MessagesBundle",currentLocale);
akf
A: 

I think the issue here is that you're trying to add a directory to the manifest Class-Path and but don't end it with a / character. From the Extension Mechanism documentation:

An application (or, more generally, JAR file) specifies the relative URLs of the optional packages (and libraries) that it needs via the manifest attribute Class-Path. This attribute lists the URLs to search for implementations of optional packages (or other libraries) if they cannot be found as optional packages installed on the host Java virtual machine*. These relative URLs may include JAR files and directories for any libraries or resources needed by the application or optional package. Relative URLs not ending with '/' are assumed to refer to JAR files. For example,

Class-Path: servlet.jar infobus.jar acme/beans.jar images/

Most people bundle their properties files in their jars. For example:

+---jar_root
      \---com
            \---proj
                   \---messages
                              Messages.class
                              MessagesBundle_en_UK.properties

This bundle would have the name "com.proj.messages.MessageBundle".

McDowell