ResourceBundles --- MissingResourceException

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
elphasg
Posts: 3
Joined: Sun Apr 23, 2023 3:07 am
Contact:

ResourceBundles --- MissingResourceException

Post by elphasg »

Can you please assist
My code is giving MissingResourceException

package com.mycompany.localize
ResourceB.java
Bundle.properties
In ResourceB.java

Code: Select all

public class ResourceB {
                   public static void main(String[] args) {
                                 String lang ="en";
                                  String country ="US"       
                                 Locale loc = new Locale(lang,country);
                                 ResourceBundle  rb = ResourceBundle.getBundle("com.mycompany.localize.Bundle", loc);
                                 System.out.println(rb.getString("wish"));
    }
}
Text in Bundles.properties
wish=Happy Birthday

What I tried already
1. Using / as file separator
2. Appending .properties on Bundle
3. localize.Bundles or localize.Bundle.properties as resource
4.Creating a resource folder com.mycompany.localize.resources
Last edited by admin on Sun Apr 23, 2023 3:28 am, edited 1 time in total.
Reason: Please put code inside [code] [/code]

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: ResourceBundles --- MissingResourceException

Post by admin »

1. The name of the file should be Bundle.properties (or Bundle_en_US.properties).
2. It should be on the classpath.

So, if you compile your code using this commad:
c:\temp>javac -d . ResourceB.java

then put Bundle.properties in c:\temp and run it like this:
c:\temp>java -classpath . com.mycompany.localize.ResourceB

This works fine.
If you like our products and services, please help us by posting your review here.

elphasg
Posts: 3
Joined: Sun Apr 23, 2023 3:07 am
Contact:

Re: ResourceBundles --- MissingResourceException

Post by elphasg »

Still not working
C:\temp>java -classpath . com.mycompany.localize.ResourceB
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name com.mycompany.localize.Bundle.properties, locale en_US
at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2045)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1683)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1586)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1549)
at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:932)
at com.mycompany.localize.ResourceB.main(ResourceB.java:21)

C:\temp>

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: ResourceBundles --- MissingResourceException

Post by admin »

It is working. I tested it. You must be doing something wrong. Did you change getBundle("com.mycompany.localize.Bundle", loc); to getBundle("Bundle", loc);
If you like our products and services, please help us by posting your review here.

elphasg
Posts: 3
Joined: Sun Apr 23, 2023 3:07 am
Contact:

Re: ResourceBundles --- MissingResourceException

Post by elphasg »

Oh thanks so much
finally Happy Birthday shows

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 33 guests