简体   繁体   中英

java.util.MissingResourceException when running a jar

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

您应该引用ResourceBundle相对于根的位置,就好像它是包的成员一样:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM