从属性文件加载字符串时出现MissingResourceException

时间:2014-08-31 15:30:23

标签: java maven resourcebundle

有人知道捆绑到.jar时找不到属性文件的原因吗? .jar运行但在尝试加载String时遇到运行时异常。

执行时的Stacktrace: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)

项目目录设置:

enter image description here

这是我从properties文件中检索字符串的类,而Messages.java位于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 + '!';
        }
    }
}

这是我的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>

1 个答案:

答案 0 :(得分:0)

这就是诀窍......

<resource>
    <directory>src/main/resources</directory>
    <excludes>
        <exclude>**/*.java</exclude>
    </excludes>
</resource>
相关问题