user5182503
user5182503

Reputation:

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?

Upvotes: 7

Views: 2969

Answers (2)

Jörg
Jörg

Reputation: 988

After trying out all options I came to the conclusion that the only reasonable way to solve this properly is to keep the localized resource bundles (*_«locale».properties) in a regular Jar rather than creating a named module. So simply create a *.jar file only containing the localized resource bundle properties and without any module-info.

I have added a note to the migration guide for Java9+ and JPMS:

https://github.com/devonfw/devon4j/blob/develop/documentation/guide-jdk.asciidoc#resourcebundles

Upvotes: 0

Alan Bateman
Alan Bateman

Reputation: 5449

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

Upvotes: 1

Related Questions