views:

685

answers:

3

I know that you can use java.util.Properties to read Java properties files.

See: http://stackoverflow.com/questions/212539/java-equivalent-to-app-config

Is there a standard place to put this file? In .NET we put application.exe.config in the same directory as application.exe. The application looks for it here by default.

Java can be made to look for a properties file in the class path but I am struggling to understand the filename/path structure to use and how to use either a standard .properties format or XML format file.

Assuming I have an API packaged in org_example_api.jar (the root package is org.example.api). I don't want to put the properties file inside the jar as it should be editable by the user. I want the user to be able to put the required configuration properties in either a .properties or .xml file somewhere relative to the classpath so I can find it without needing to know anything about the ir file system structure.

Will this work on all systems:

/classpath/org_example_api.jar
/classpath/org/example/api/config.properties OR
/classpath/org/example/api/config.xml

Code:

java.util.Properties = ? //NEED SOME HELP HERE
+1  A: 

The world is wide open to you here. The only best practice is what works best for you:

  1. Whatever program the user is running can require the path to the properties file as an argument
  2. Your application can be configured to look in the current directory for config.properties.
  3. If the file can't be found, you could maybe fall back to the user.home directory, or fall back to wherever your application is installed.

Personally I usually have my applications attempt to read properties files from the classpath - but I'm not in a world where I have end-users update/change the file.

Whatever option you choose, just make sure you clearly document it for your users so they know which file to edit and where it needs to be!

matt b
+2  A: 

This purely depends on the type of application you are developing.

1) If it is a web application the best place is inside the WEB-INF/classes/ folder.

2) If you are developing a standalone application there are many approaches. From your example I think the following structure will work.

/<dist>/org_example_api.jar
/<dist>/config.xml
/<dist>/run.sh

In the run.sh you can start the java application providing the current directory also in the classpath. Something like this.

java -cp .:org_example_api.jar ClassToExecute

3) If it is an API distribution it is up to the end user. You can tell the user that they can provide the config.xml in the classpath which should follow some predefined structure. You can look at Log4J as an example in this case.

Jerrish Varghese
+1  A: 

You can put the properties file in a directory or JAR in your CLASSPATH, and then use

InputStream is = getClass().getResourceAsStream("/path/goes/here");
Properties props = new Properties();
props.load(is);

(I noticed you mentioned this in your OP, but others may find the code useful.)

joev
getClassLoader() is probably redundant, as Class<T> has getResourceAsStream() .
R. Bemrose
Thanks! Edited my answer accordingly.
joev