我有一段用于打开和读取属性文件的代码:
public static Properties getProperties(String path) {
ClassLoader classLoader = PropertiesLoader.class.getClassLoader();
FileInputStream file = null;
try {
if (classLoader.getResource(path) == null) {
log.error("Can't get resource {} from classLoader", path);
return null;
}
file = new FileInputStream(classLoader.getResource(path).getFile());
Properties properties = new Properties();
properties.load(file);
return properties;
} catch (FileNotFoundException e) {
log.error("Property file {} not found with exception: \n {}",
path,
e.getMessage()
);
} catch (IOException e) {
log.error("Can't load property file {} with exception: \n {}",
path,
e.getMessage()
);
}
return null;
}
我通过mvn运行课程
mvn exec:java -Dexec.mainClass="edu.garmr.sniffer.Sniffer"
eveything还可以,但是当我尝试使用以下方式编译带有依赖项的jar文件时:
mvn clean编译程序集:单个
并直接从bash运行我的类我有以下错误:
login@ubuntu:~/garmr$ java -cp ./target/garmr-1.0-SNAPSHOT-jar-with-dependencies.jar 'edu.garmr.sniffer.Sniffer'
2015-05-21 09:56:37 ERROR PropertiesLoader:37 - Property file sniffer.properties not found with exception:
file:/home/login/garmr/target/garmr-1.0-SNAPSHOT-jar-with-dependencies.jar!/sniffer.properties (No such file or directory)
Exception in thread "main" java.lang.NullPointerException
at edu.garmr.sniffer.Sniffer.main(Sniffer.java:87)
我尝试使用mc打开jar文件,jar的根目录下有这样的文件。