wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes ResourceBundle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ResourceBundle" Watch "ResourceBundle" New topic
Author

ResourceBundle

Pavan Chillara
Greenhorn

Joined: Sep 27, 2006
Posts: 14
Hi,

I have written a java class involving ResourceBundle.In main() method I have created an instance for ResourceBundle using getBundle() with parameters Name of the class in String format and Locale Object.This class is in the same package where my java program is.But it is giving MissingResourceException when i run this.Can any one explain me the reason and guide me how to execute this.

The code for my class and the class which i am calling
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

It is because your ResourceBundle class is an inner class. ResourceBundles need to be public outer classes. Move your ButtonLabel_en class to its own file (ButtonLabel_en.java), make it public, compile both classes, and it will work.

ButtonLabel_en.java
Pavan Chillara
Greenhorn

Joined: Sep 27, 2006
Posts: 14
Hi,

I did that in the same way.I have created a new java file ButtonLabel_en.java and made that class public.I have kept that in the same package where my ResourceBundleEx.java is existing.

But still i am getting the same Exception.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624


...I have kept that in the same package where my ResourceBundleEx.java is existing...

The above statement is the key to why the ResourceBundle is not being found.

The getBundle method basically searches the classpath for the bundle you named. It ultimately does this using a ClassLoader. It simply does some additonal work to determine the localized name of the class. Let's say "appBaseDir" is in my classpath, and my two compiled classes are in the com.foo package, which are in the appBaseDir:



If my code says:

I have ultimately told the the ResourceBundle class to look for a class named "ButtonLabel_en" on the classpath. So it looks at the classpath, and finds the directory "appBaseDir" and searches that directory for the a class name "ButtonLabel_en" (i.e. the file "ButtonLabel_en.class"). Well there is no "ButtonLabel_en.class" file in the "appBaseDir" directory. The only thing that exists within the directory "appBaseDir" is another directory named "com".

Now, if, as an exercise, I put in my ButtonLabel_en Class, which is in the com.foo package, the following code in a non static method:

or the code

in a static method, and have the code execute, I would see that the full name of my class is "com.foo.ButtonLabel_en", not just simply "ButtonLabel_en". I therefore need to tell the ResourceBundle.getBundle() method the full name of the Bundle I want to load, so it can "translate" that bundle name and locale into the full name of the class to look for:

I have now told the loader to look for the class "com.foo.ButtonLabel_en" on the classpath. The loader "knows" that a package means a directory structure, so it will now look for the class ButtonLabel_en inside a directory named "foo", inside a directory named "com", inside a directory on the classpath. In other words, it will search for the file com/foo/ButtonLabel_en.class within all the directories on the classpath.

Up to this point, this is basically how the ClassLoader would search the classpath for a class (or resource). The ResourceBundle.getBundle() method just goes one step farther. If it can't find the class com/foo/ButtonLabel_en on the classpath, it will look for com/foo/ButtonLabel as part of its search process.

I hope that not only answers your question and solves your problem, but also helps you understand what is happening behind the scenes a little so that you understand why the issue was occurring.
[ November 13, 2006: Message edited by: Mark Vedder ]
Pavan Chillara
Greenhorn

Joined: Sep 27, 2006
Posts: 14
Thank you Mark Vedder,

Now I am clear on what is happening inside.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ResourceBundle
 
Similar Threads
I18n problem
Internationalization
Can't display chinese and japanese at the same time (Urgent!!!!)
Posting it again (Urgent!) Can't display chinese and japanese font at the same time
Internationalization