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 <copy>
task after <javac>
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 havebuild/classes/connection.properties.
- If your code is like
ResourceBundle.getBundle("com.javahowto.test.connection"),
then after copying you should havebuild/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"),
thenconnection-info.jar
should contain this entry:connection.properties
. - If your code is like
ResourceBundle.getBundle("com.javahowto.test.connection")
, thenconnection-info.jar
should contain this entry:com/javahowto/test/connection.properties
.
- Or you can choose to put the resource in a separate
resources
directory, includeresources
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 haveresources/connection.properties
. - If your code is like
ResourceBundle.getBundle("com.javahowto.test.connection")
, then you should haveresources/com/javahowto/test/connection.properties
.
tags: Java, MissingResourceException, debug, ResourceBundle