1

For some reason the messages.properties file is located somewhere in WEB-INF folder but outside WEB-INF/classes folder. Specifically the file's path is /WEB-INF/messages/messages.properties. How do I load this resource bundle using the method ResourceBundle.getBundle( ? )?

supertonsky
  • 2,563
  • 6
  • 38
  • 68
  • This might help: http://stackoverflow.com/questions/1172424/how-to-load-a-resource-bundle-from-a-file-resource-in-java – home Nov 30 '12 at 06:09
  • since only classes folder is in classpath, only possible way I see is to override Tomcat class loader some way. Easy way is to put messages folder in classes dir . – Subin Sebastian Nov 30 '12 at 06:33

2 Answers2

1

You can get the path using the Servlet context as follows;

getServletContext().getResource("/messages/messages.properties).getPath();

then using the URLClassLoader() create a class loader and pass this on to the getBundle() method.

dinukadev
  • 2,279
  • 17
  • 22
0

I just put together a ResourceBundle.Control extension that handles this exact situation:

https://gist.github.com/Tzrlk/82d74c074e63955a8a35

usage:

ServletContext servletContext = //! get access to servlet context instance
Locale locale = //! get access to locale instance

try {
    ResourceBundle.Control control = new ServletContextBundleControl(servletContext);
    ResourceBundle bundle = ResourceBundle.getBundle("/WEB-INF/messages/messages", locale, control);
} catch (MissingResourceException error) {
    // handle exception 
}
tzrlk
  • 848
  • 1
  • 13
  • 30