I have piece of code for opening and reading from properties files:
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;
}
While I running my class via mvn
mvn exec:java -Dexec.mainClass="edu.garmr.sniffer.Sniffer"
eveything is okay, but when I try to compile a jar-file with dependencies following way:
mvn clean compile assembly:single
and run my class directly from bash I have following error:
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)
I tried to use mc for opening jar file and there is such file at the root of jar.