0

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.

Mikhail
  • 809
  • 8
  • 16
  • Are you able to reproduce this without getting Maven involved at all? A short but complete program demonstrating the problem would really help... – Jon Skeet May 21 '15 at 17:50
  • I don't think `FileInputStream` is appropriate for a resource from a jar -- You can just use `GetResourceAsStream` and load it directly from the returned InputStream – Gus May 21 '15 at 17:57
  • use [`Properties#load(InputStream)`](http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load%28java.io.InputStream%29) instead – jmj May 21 '15 at 18:26

0 Answers0