0

I've written a class that loads external properties for use in my program, it is modeled off of this link: Load Properties File in Singleton Class. I've read here (FileNotFoundException with properties file) and here (Java Properties File not loading) of how to load an external property file, however I continue to get a FileNotFoundException. My properties file is external and is located in the same directory as my executable jar.

public class ExternalProperties extends Properties{

private static ExternalProperties instance = null;
private Properties properties;
private static String location;

protected ExternalProperties() throws IOException {
    properties = new Properties();
    properties.load(getClass().getResourceAsStream("test.properties"));
}

public static ExternalProperties getInstance(){
    if(instance == null){
        try{
            instance = new ExternalProperties();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return instance;
}

public static void setLocation(String path){
    location = path;
}

}

I am passing the location of the property file through the command line such as:

java -jar chef-deploy-tests-0.0.0009-SNAPSHOT-jar-with-dependencies.jar test.properties

Any suggestions on what I am doing wrong?

2 Answers 2

0

It depends on your location of the test.properties file.

 * <li> If the name begins with a {@code '/'}
 * then the absolute name of the resource is the
 * portion of the name following the {@code '/'}.
 *
 * <li> Otherwise, the absolute name is of the following form:
 *
 * <blockquote>
 *   {@code modified_package_name/name}
 * </blockquote>
 *
 * <p> Where the {@code modified_package_name} is the package name of this
 * object with {@code '/'} substituted for {@code '.'}.
Sign up to request clarification or add additional context in comments.

Comments

0

I think your problem is that test.properties is NOT on your classpath. The way you are running it now is simply passing the file as an argument to the application. I think what you mean to do is add it to the application's classpath:

java -jar chef-deploy-tests-0.0.0009-SNAPSHOT-jar-with-dependencies.jar -classpath .

This will include all the files in the the current directory in the java classpath. Another option may be to put that test.properties file into a separate directory and specify that one instead of the '.'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.