jdub7
jdub7

Reputation: 25

Java - Loading External Properties File - FileNotFoundException

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?

Upvotes: 0

Views: 1387

Answers (2)

Rob Whiteside
Rob Whiteside

Reputation: 58

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 '.'

Upvotes: 0

sendon1982
sendon1982

Reputation: 11284

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 '.'}.

Upvotes: 0

Related Questions