Tuesday, July 28, 2009

log4J internationalization & localisation

http://java.sun.com/developer/technicalArticles/Intl/ResourceBundles/
http://java.sun.com/docs/books/tutorial/i18n/index.html
http://forums.sun.com/thread.jspa?threadID=5370732&tstart=60
http://blogs.sun.com/CoreJavaTechTips/entry/logging_localized_message

Log4J internationalization
http://forums.sun.com/thread.jspa?threadID=770074



Debug java util MissingResourceException

java.util.MissingResourceException Can't find bundle for base name, locale...How did I get this exception? The crux of this problem is the requested resource, in most cases, a properties file, is not configured correctly in the classpath. For example, you have a properties file, connection.properties, in the same source directory as Java source files. Javac will compile *.java into *.class in a target directory such as build/classes, which is in your runtime classpath. But connection.properties is not copied into build/classes directory unless you either add a task after in the Ant build file, or do so manually.

How to fix it? Make sure this resource is configured correctly in the classpath through one of the following:

  • Like I said above, copy the resource from source directory to build/classes directory, which is in the classpath.
    • If your code is like ResourceBundle.getBundle("connection"), then after copying you should have build/classes/connection.properties.
    • If your code is like ResourceBundle.getBundle("com.javahowto.test.connection"), then after copying you should have build/classes/com/javahowto/test/connection.properties.
  • Or you can choose package resources into a jar file, say, connection-info.jar, which is included in runtime classpath (not needed in Javac classpath).
    • If your code is like ResourceBundle.getBundle("connection"), then connection-info.jar should contain this entry: connection.properties.
    • If your code is like ResourceBundle.getBundle("com.javahowto.test.connection"), then connection-info.jar should contain this entry: com/javahowto/test/connection.properties.
  • Or you can choose to put the resource in a separate resources directory, include resources directory in runtime classpath. This way you don't have to duplicate the resource in multiple directories/jar. The disadvantage is it's a little inconvenient at development time to have resource in a separate directory than Java code.
    • If your code is like ResourceBundle.getBundle("connection"), then you should have resources/connection.properties.
    • If your code is like ResourceBundle.getBundle("com.javahowto.test.connection"), then you should have resources/com/javahowto/test/connection.properties.

No comments:

Post a Comment