Search code examples
javaclassloaderresourcebundle

How to properly setup ResourceBundle and ClassLoader?


I want to pass the correct ResourceBundle to my Jasper report, but I'm not sure exactly how to do that. This is my code so far:

Locale locale = Locale.getDefault();
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("Bundle.properties").getFile()); 
URL[] urls = {file.toURI().toURL()}; 
ClassLoader loader = new URLClassLoader(urls); 
ResourceBundle rb = ResourceBundle.getBundle("bundle", locale, loader);

But this gives me the

java.util.MissingResourceException: Can't find bundle for base name bundle, locale XX_XX

error :(

My bundle file are here:

src/main/resources/Bundle.properties
src/main/resources/bundle_en.properties
src/main/resources/bundle_de.properties

Thank you!


Solution

  • Firstly, I think you should use classLoader, not loader. I'm guessing loader is just a failed workaround, so I suggest you remove it.

    Secondly, the fallback bundle file should probably be called bundle.properties, not Bundle.properties.