Pavel_K Pavel_K - 27 days ago 9
Java Question

How to get ResourceBundle from another Module in Java 9?

I have two modules: module-a and module-b. Module-a has properties file (

com/foo/texts_en.properties
). Module-a exports
com.foo
package. In module-b I want to get this resource. For this I do the following in module-b:

Module moduleA = ClassFromModuleA.class.getModule();
ResourceBundle resourceBundle = ResourceBundle.getBundle("com/foo/texts",
Locale.ENGLISH, moduleA.getClassLoader());
System.out.println("TEST :" + resourceBundle.getString(key);


This is what I get:

Caused by: java.util.MissingResourceException: Can't find bundle for base name com/foo/texts, locale en
at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2045)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1679)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1572)
at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:1273)


What is my mistake?

Answer Source

The "Resource Bundles in Named Modules" section of ResourceBundle provides the details on how resources in named modules are located.