MissingResourceException

java.util.MissingResourceException – How to resolve Missing Resource Exception

In this example we will discuss about MissingResourceException. This exception is a runtime exception, and it’s thrown when a resource is missing.

MissingResourceException extends RuntimeException which is the base class of all exceptions that can be thrown during the normal operation of Java Virtual Machine. By extending RuntimeException, we don’t have to catch MissingResourceException in the code, neither to declare main as throws clause.

MissingResourceException exists since JDK 1.1.
 
 

The structure of MissingResourceException

Constructor

  • MissingResourceException(String s, String className, String key)

    Constructs a MissingResourceException with the specified information.

The MissingResourceException in Java

Resource files are files that have the .property extension and hold some data. Mostly, they are used as a way to store strings in order to achieve internationality for the application.

To see when the MissingResourceException is thrown, create a class called MissingResourceFile with this source code:

MissingResourceFile.java

package com.javacodegeeks.examples;

import java.util.ResourceBundle;

public class MissingResourceFile {

	public static void main(String[] args) {
		//this will throw the exception
		ResourceBundle myResources = ResourceBundle.getBundle("MyResources");
		
	}

}

In this example, I try get the resources called “MyResource”, which in fact doesn’t exist.

When you run the code, you will get this output:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name MyResources, locale en_US
	at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
	at java.util.ResourceBundle.getBundleImpl(Unknown Source)
	at java.util.ResourceBundle.getBundle(Unknown Source)
	at com.javacodegeeks.examples.MissingResourceFile.main(MissingResourceFile.java:9)

This output was expected, since there is no resource file called “MyResources” in our project.

The same would happen if the resource file exists, but the resource required does not exist. To see this, create a Java class called MissingResource with this source code:

MissingResource.java

package com.javacodegeeks.examples;

import java.util.ResourceBundle;

public class MissingResource {

	public static void main(String[] args) {
		ResourceBundle myResources = 
						ResourceBundle.getBundle("com.javacodegeeks.examples.resources");
		String name = myResources.getString("name");
		String surname = myResources.getString("surname");
		String age = myResources.getString("age");
		System.out.println(String.format("Hello! I'm %s %s, %s years old",name, surname, age));
	}

}

Also, put this resource file, called “resources.property”, in the same directory as MissingResource:

resources.property

name = John

surname = Doe

I access the file with the proper name, and then try to get three strings, by using their keys.

You can see that I am requiring an inexistent key, the “age” key. This will throw the exception. So, when I try to run it, I get this output:

Exception in thread "main" java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key age
	at java.util.ResourceBundle.getObject(Unknown Source)
	at java.util.ResourceBundle.getString(Unknown Source)
	at com.javacodegeeks.examples.MissingResource.main(MissingResource.java:12)

More about MissingResourceException

MissingResourceException is usually thrown when the resource required by the application is not found. This means that the resource file is not there, or that the key which we are trying to get is not there.

How to deal with MissingResourceException

When you encounter on a MissingResourceException, you should firstly recognize if the problem is with the resource file name, its location, or with any key which is being accessed by the program.

After you do this, then you put the proper file name, or the proper key, or put the required key-value pair in the .property file.

Download Code

Download
You can download the full source code of this example here : MissingResourceExceptionExample

Aldo Ziflaj

Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also one of the co-founders of Things Lab.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button