Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从jar文件删除属性文件时出错_Java_Properties_Embedded Resource - Fatal编程技术网

Java 从jar文件删除属性文件时出错

Java 从jar文件删除属性文件时出错,java,properties,embedded-resource,Java,Properties,Embedded Resource,我试图从jar文件中加载属性文件,但无法这样做。 下面是我正在加载文件的类的代码 public class PropertyClass { private static Properties properties; public String getProperty(String propertyName) { try { InputStream inputStream = this.getClass().getClassLoade

我试图从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行- 属性。加载(inputStream)


在调用
Properties.load()
之前,您需要初始化
Properties
对象,然后才能获得此方面的任何帮助:


在调用
Properties.load()
之前,需要初始化
Properties
对象:

private static Properties properties = new Properties();