繁体   English   中英

从jar文件中填充属性文件时出错

[英]Error while laoding properties file from jar file

我正在尝试从jar文件中加载属性文件,但无法这样做。 以下是我正在加载文件的类的代码

public class PropertyClass {
    private static Properties properties;

    public String getProperty(String propertyName)  {
        try {
            InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("resources/test.properties");

            System.out.println("Input Stream " + inputStream);
            properties.load(inputStream);

            inputStream.close();
            if (properties == null) {
                System.out.println("Properties null");
            }
        } catch (IOException e){
            e.printStackTrace();
        }
        return properties.getProperty(propertyName);
    }
}

类文件和属性文件都打包在jar中。 但是当我尝试从另一种方法加载文件时,出现以下错误:-

Input Stream - sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@9304b1
Exception in thread "main" java.lang.NullPointerException
at PropertyClass.getProperty(PropertyClass.java:16)

它不会将输入流显示为null。接下来的一行是我的代码中的第16行-properties.load(inputStream);

任何对此的帮助

您需要先初始化Properties对象,然后才能调用properties.load()

private static Properties properties = new Properties();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM