无法检索maven资源中的属性文件

时间:2017-08-16 03:24:09

标签: java maven properties nullpointerexception resources

public class LanguageUtil {

    public static Properties properties = new Properties();

    private void loadLanguageConfig(String filename) {

        try {
            String resourceName = "lang\\" + filename; 
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
                properties.load(resourceStream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

错误:

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at <package>.testing.Main.main(Main.java:26)
Caused by: java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Unknown Source)
        at java.util.Properties.load0(Unknown Source)
        at java.util.Properties.load(Unknown Source)
        at <package>.LanguageUtil.loadLanguageConfig(LanguageUtil.java:19)
        ... 5 more

第19行是“properties.load(resourceStream);”

我正在尝试从maven创建的资源文件夹中的“lang”文件夹中检索文件。当jar构建时,我打开jar,lang文件夹以及lang文件夹中的文件就在那里。

编辑:感谢您https://stackoverflow.com/users/679824/krishnan-mahadevan的帮助!将“lang \”的文件夹路径更改为“lang /”修复了问题。

1 个答案:

答案 0 :(得分:0)

通过将“lang”重命名为“lang /”

来修复