Java 从jar文件删除属性文件时出错
我试图从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
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();