Question

Please tell me where exactly do i put my .properties file in my eclipse project. Do I make a separate folder for it ? I want to put it in such a way that I will be able to distribute my project easily in JAR form.

Thanks.

EDIT:

I want to put the properties file in such a way that it can be easily edited later, on any OS.

Was it helpful?

Solution

The approach I use in order to be able to easily change configuration without redeployment.

One version of the property file (with the defaults) in the root of your project, I always let it in the application package (foo.bar.myapp). I load the default one with getResourceAsStream("/foo/bar/myapp/config.properties") or like in the sample relative to the class package.

And additionally I ready a system property:

String configFileLocation = System.getProperty("config");

And just override the default with the properties read from the config file passed as property.

For instance:

Properties props = new Properties();
props.load(Main.class.getResourceAsStream("mydefault.properties"));

System.out.println("default loaded: " + props);

String configFile = System.getProperty("config");
if (configFile != null) {
    props.load(new FileInputStream(configFile));
    System.out.println("custom config loaded from " + configFile);
}

System.out.println("custom override: " + props);

This would load first your resource stored under your project in foo.bar package named mydefault.properties, and after it if system property config is configured it will load override the loaded properties with the one the the referred path.

The system property can be set using -D parameter, in this case would be something like: java -Dconfig=/home/user/custom.properties foo.bar.Main. Or if you are in a web application (Tomcat for instance) you can set this property using CATALINA_OPTS.

OTHER TIPS

src/main/resource would be an ideal choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top