0

In my program, a Java8 application library, written on NetBeans, when I go to the dist folder and run Project.jar using the command:

java -jar Project.jar

I get the following error:

Sorry, unable to find config.properties
Exception in thread "main" java.lang.NullPointerException

It seems it cannot create the input stream necessary to read the properties file.

Here is the class I use to read the properties:

public class PropertyAccess {

    public static String getPropertyFromFile(String propertyName){
        String ret = "";
        //System.out.println(propertyName);
        InputStream input = null;
        Properties prop  = new Properties();

        try {
            String filename = "config.properties";
            input = PropertyAccess.class.getResourceAsStream(filename);
            if(input == null) {
                    System.out.println("Sorry, unable to find " + filename);
            }

            //input = PropertyAccess.class.getResourceAsStream("/config/config.properties");
            //input = new FileInputStream("config/config.properties");

            input = PropertyAccess.class.getClassLoader().getResourceAsStream("config/config.properties");

            prop.load(input);
            ret =  prop.getProperty(propertyName);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return ret;
    }
}

There is a bad design/implementation where I load the prop file every time I need to read a property from file, this will be corrected before the main release, but this is not what is causing the issue.

Can someone explain how to fix this?

I've tried to add the file to the classpath, but it also doesnt work.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
lrente
  • 1,070
  • 1
  • 9
  • 27
  • 1
    How to fix? Make sure both file `config.properties` and `config/config.properties` are in the jar file *(why you have two files is a head-scratcher, but whatever you need)*. Well, the exact path depends on the package of class `PropertyAccess`, which we don't know. – Andreas Mar 05 '18 at 22:33
  • Where exactly is your properties file located in relation to the root of the project? – Tim Strawbridge Mar 05 '18 at 22:38

0 Answers0