简体   繁体   中英

Loading a Java .properties file, getting java.util.MissingResourceException: Can't find bundle for base name what am I doing wrong?

I'm new to loading a custom.properties file and am doing so from within my Spring MVC app.

I have a properties file at: com.company.wtwebapp.properties.wtwebapp.properties

I have tried as below as well as a variety of combinations (leaving out the src.main.java, using "/" instead of ".") but nothing works.

          ResourceBundle rb = ResourceBundle.getBundle
          ("src.main.java.com.company.wtwebapp.properties.wtwebapp.properties");

On my classpath file I have:

<classpathentry kind="lib" path="src/main/java/com/company/wtwebapp/properties/wtwebapp.properties"/>

I always get the error:

java.util.MissingResourceException: Can't find bundle for base name src.main.java.com.company.wtwebapp.properties.wtwebapp.properties, locale en_US

I would appreciate any advice on the proper way / better way to accomplish the loading of the properties file. I have been doing a lot of searches but they are just leading to more confusion as from what I can tell what I am doing should be working (but obviously something is wrong). Thanks

From the javadoc:

getBundle attempts to locate a property resource file. It generates a path name from the candidate bundle name by replacing all "." characters with "/" and appending the string ".properties".

You are supposed to provide the basename only; remove the .properties suffix. Also if the bundle is in the same directory as the class, remove all the path information.

Try loading your resource bundle like this:

ResourceBundle rb = ResourceBundle.getBundle("com.company.wtwebapp.properties.wtwebapp");

And modify your classpath entry like so:

<classpathentry kind="lib" path="src/main/java"/>

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