Search code examples
javamavenresourcebundle

MissingResourceException when loading string from properties file


Does anyone know why the properties file is not found when bundled into a .jar?
The .jar runs but encounters a runtime exception when trying to load a String.

Stacktrace when executing: Messages.getString("ui.frameTitle") ..

Caused by: java.util.MissingResourceException: 
Can't find bundle for base name com.justc0de.tools.messages, locale en_US
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1525)

Project directory setup:

enter image description here

This is my class for retrieving strings from the properties file, and Messages.java lives inside src/main/java/:

package com.justc0de.tools;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Messages {
    private static final String BUNDLE_NAME = "com.justc0de.tools.messages"; //$NON-NLS-1$

    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

    private Messages() {
    }

    public static String getString(String key) {
        try {
            return RESOURCE_BUNDLE.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }
}

This is my POM.xml ..

<project 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>app</groupId>
    <artifactId>app</artifactId>
    <version>0.1</version>
    <name>app</name>
    <description>app_packaged</description>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>  <!-- this is used for inheritance merges -->
                        <phase>verify</phase>  <!-- bind to the verify phase, to execute after package phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <programs>
                        <program>
                            <mainClass>com.justc0de.game.Main</mainClass>
                            <id>thegame</id>
                        </program>
                    </programs>
                    <repositoryName>lib</repositoryName>
                    <repositoryLayout>flat</repositoryLayout>
                    <assembleDirectory>${project.build.directory}/${project.artifactId}</assembleDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>prepare-assembly</id>  <!-- this is used for inheritance merges -->
                        <phase>package</phase>  <!-- bind to the package phase -->
                        <goals>
                            <goal>assemble</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.jogamp.gluegen</groupId>
            <artifactId>gluegen-rt-main</artifactId>
            <version>2.1.5-01</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

Solution

  • This did the trick ..

    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>**/*.java</exclude>
        </excludes>
    </resource>