views:

443

answers:

6

What is the best way to store and load application level properties in Java.

Is there anything simulare to .net where I would just put something in the app.config

 <appSettings>
    <add key="emailAddress" value="[email protected]" />
  </appSettings>

And then retrieve it like this:

string AdminEmail = ConfigurationManager.AppSettings["emailAddress"];

Do I have to load properties files into file streams and such? seems like unnecessary work.

I have tried this:

Properties props = new Properties();

FileInputStream fis = new FileInputStream("myProps.properties");
props.load(fis);
fis.close();

But this is give me a java.io.FileNotFoundException exception. Where does the properties file need to go to in relation to the class?

A: 

The file path it would be looking for in that case is relative to where you started your java application from. This is not where the main class is or the jar file but where you called Java from. If you are starting your application with a script that calls Java, then it is that directory.

Say for example, you application is bundled in a jar file 'app.jar'. Put 'myProps.properties' in the same directory and run 'java -jar app.jar' from that directory. It should find your properties file that way.

Chris Dail
+1  A: 

Typically it will attempt to load from the application's current directory, which can vary depending on how you run it. You should be able to determine this at runtime by doing:

String currentFolder = System.getProperty("user.dir");
Jason Nichols
+1  A: 

If you use a FileInputStream like that, your path is relative to the current directory of the OS, which usually is the startup directory.

If you instead use the Java built in resources mechanism (as described in the API, getResourceAsStream() et al), the path will be relative to the location of your class. With this approach you can also load resources from within jars and even over networks (for Applets for instance). The concept which is used is a sort of virtual filesystem, which is called the 'classpath' in Java jargon. There is a devx article covering it a litte more in detail.

In short, this sort of code works well:

Properties prop = new Properties();
//with properties in the same dir as current class
prop.load(getClass().getResourceAsStream("my.properties")); 
//with properties in the root dir of your jar, or in base of classpath
prop.load(getClass().getResourceAsStream("/my.properties"));

You will need to add error handling...

disown
A: 

You can use Properties with a ResourceBundle. I use this in a application to store labels, buttons and messages in different languages

First you create a properties file, like test.properties. It´s a text file and inside it you put your information like this:

propertyname=value

In your case

[email protected]
[email protected]

and so on...

To get this properties in the code, create a ResourceBundle object with the name of your property file to call the properties.

ResourceBundle rb = ResourceBundle.getBundle("test");

To get an specific value from a properties file, just call the ResourceBundle

String value = rb.getString("emailAddress");

This way, the String named value contains the value of the property named "emailAddress", located in the test.properties file

String value2 = rb.getString("email2");

Likewise, the String named value2 contains the value of the property named "email2", located in the test.properties file

marionmaiden
+3  A: 

The Preferences API provides this functionality. It has many warts, but if you are looking to do this in an OS-agnostic fashion, this is the only way to accomplish this using the standard Java runtime libraries. You can of course always write your own OS-specific code to meet your needs. Yes, you can write simple code to load a properties file, but the location of that file can become a problem across multiple operating systems. I assume since you are writing a desktop app in Java, you care about OS portability. Otherwise Java might not be the best choice for a desktop app.

Todd Stout
A: 

When you do not specify an absolute path, the one chosen is the current one.

It's not exactly what you asked, but if you want to use XML files as configuration, you could have a look at Apache Commons Configuration

Valentin Rocher