Blog |

How to Fix the Missing Resource Exception in Java

How to Fix the Missing Resource Exception in Java
Table of Contents

The MissingResourceException is an unchecked exception in Java that occurs when a resource is missing. Since the MissingResourceException is thrown at runtime, it does not need to be declared in the throws clause of a method or constructor.

 

What Causes MissingResourceException

The MissingResourceException occurs when a resource required by a Java program is not found. Common causes for this include the following:

  • If a resource file with the specified name does not exist or could not be found.
  • Attempting to retrieve a value associated with a key in a resource file when no value was defined in the file with the specified key.
  • Attempting to retrieve the country or language of a Locale if no three-letter abbreviation could be found for the country or language of that Locale.

 

MissingResourceException Example

Here is an example of an MissingResourceException thrown when a resource file could not be found:

public class MissingResourceExceptionExample {
    public static void main(String args[]) {
        ResourceBundle myResources = ResourceBundle.getBundle("resources");
    }
}

In the above example, a resource file with the name resources.properties is attempted to be retrieved. Since no file with this name exists, running the above code throws the MissingResourceException:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name resources, locale en_GB
    at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2055)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1689)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593)
    at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1556)
    at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:857)
    at MissingResourceExceptionExample.main(MissingResourceExceptionExample.java:5)

 

How to Fix MissingResourceException

To fix the MissingResourceException, it should be ensured that any resource required by the program exists with the correct name and at the right location. Any values attempted to be retrieved from a resource file using a key should exist with the right key.

If a Locale object is used and its country or language is retrieved, it should be ensured that the object is initialized with valid country and language codes.

In the earlier example, the exception can be resolved by creating a resources.properties file in the same directory as the Java class. Running the code after creating the file will execute correctly and not throw a MissingResourceException.

 

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape