Specify Java localization file

I am trying to use the ResourceBundle class to retrieve the locale-specific text for my application, but I encounter some errors.

java.lang.ClassCastException: org.project.MyClass cannot be cast to ResourceBundle java.util.MissingResourceException: Can't find bundle for base name org.project.MyClass, locale en_US 

The code I use to create the ResourceBundle looks like this:

 static final ResourceBundle i18ln = ResourceBundle.getBundle("org.project.MyClass", Locale.getDefault()); 

I searched for about an hour on how to specify the location of a resource file without success. My project is configured as follows:

  project-folder /
     src /
         org /
             project /
                 Myclass.java
     test /
     lib /
     res /
         images /
         org /
             project /
                 MyClass.properties

Is my project structure simply not compatible with the Java ResourceBundle? Does anyone know how I can make this work?

+4
source share
4 answers

Is MyClass.java some kind of subclass of ResourceBundle? I suspect this is not the case, therefore ClassCastException.

If MyClass is not something like a ResourceBundle, try renaming MyClass.properties to something else, such as MyResource.properties, and put your res directory in the classpath to find your resource. Make sure you change the name of your resource when using ResourceBundle.getResource as follows:

 static final ResourceBundle i18ln = ResourceBundle.getBundle("org.project.MyResource", Locale.getDefault()); 
+4
source

ResourceBundle will try to instantiate a class called org.project.MyClass before it tries to load the properties file. Once upon a time it was quite normal when people compiled their code into specific classes, but you don’t see it very often.

The documentation for resource packages is described in javadoc .

  • First, it tries to load the class using the name candidate package. If such a class can be found and loaded using the specified loader class, is compatible with the ResourceBundle, is accessible from the ResourceBundle, and there may be an instance, getBundle creates a new instance of this class and uses it as a result resource package.
  • Otherwise, getBundle tries to find the resource resource file. This generates the path name from the candidate name, replacing all the “characters with the“ / ”character and adding the string“ .properties. ”He tries to find the“ resource ”with this name using ClassLoader.getResource. (Note that the“ resource ”in the sense of getResource does not have nothing to do with the contents of the resource package, it's just a data container, such as a file.) If it finds a "resource", it tries to create a new PropertyResourceBundle from its contents. If successful, this instance becomes a bundle result resource.

If a Resource Resource Pack was not found, a MissingResourceException was thrown.

By convention, many projects are consistent with the corresponding package name for property files. For example, the package foo.bar will have the accompanying package foo.bar.nls for l10n resources.

+2
source

You have MyClass.properties in the res folder. Copy MyClass.properties to the src folder, it should work.

0
source

This problem may also occur if the case of the properties file and the class do not match. For example: MyClass.java and mcyclass.properties .

It looks like a platform; in my situation, I was able to create locally on my Mac, but when I clicked on the Team City Linux build agent, the build failed.

0
source

All Articles