26

Is there a Java equivalent to .NET's App.Config?

If not is there a standard way to keep you application settings, so that they can be changed after an app has been distributed?

John Topley
  • 113,588
  • 46
  • 195
  • 237
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238

3 Answers3

18

For WebApps, web.xml can be used to store application settings.

Other than that, you can use the Properties class to read and write properties files.

You may also want to look at the Preferences class, which is used to read and write system and user preferences. It's an abstract class, but you can get appropriate objects using the userNodeForPackage(ClassName.class) and systemNodeForPackage(ClassName.class).

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • Can you give some more information on the first option: "web.xml can be used to store application settings"? Are you suggesting storing the application settings as `context-param`s? Or something else? – theyuv Apr 27 '16 at 10:49
  • There are a few different types of params you can put in web.xml, but I probably did mean `context-param`. I don't remember as it's been 7.5 years since then. – Powerlord Apr 28 '16 at 22:43
11

To put @Powerlord's suggestion (+1) of using the Properties class into example code:

public class SomeClass {
    public static void main(String[] args){
        String dbUrl = "";
        String dbLogin = "";
        String dbPassword = "";     
        if (args.length<3) {
            //If no inputs passed in, look for a configuration file
            URL configFile = SomeClass.class.getClass().getResource("/Configuration.cnf");
            try {
                InputStream configFileStream = configFile.openStream();
                Properties p = new Properties();
                p.load(configFileStream);
                configFileStream.close();

                dbUrl      = (String)p.get("dbUrl");
                dbLogin    = (String)p.get("dbUser");
                dbPassword = (String)p.get("dbPassword");               
            } catch (Exception e) {  //IO or NullPointer exceptions possible in block above
                System.out.println("Useful message");
                System.exit(1);
            }
        } else {
            //Read required inputs from "args"
            dbUrl      = args[0];
            dbLogin    = args[1];
            dbPassword = args[2];           
        }
        //Input checking one three items here
        //Real work here.
    }
}

Then, at the root of the container (e.g. top of a jar file) place a file Configuration.cnf with the following content:

#Comments describing the file
#more comments
dbUser=username
dbPassword=password
dbUrl=jdbc\:mysql\://servername/databasename

This feel not perfect (I'd be interested to hear improvements) but good enough for my current needs.

Pursuit
  • 12,285
  • 1
  • 25
  • 41
  • Seriously? So you pretty much run the stream reader over the file to get the settings? In the case of multiple classes you will need something like a helper class that can be used across? Is this the best solution Java can offer here??? It just seems very naive. – tom33pr Oct 26 '15 at 15:27
  • This is the best I could manage, for my needs, in 2013. I believe that the more general solution is generally wrapped up in an application server. The phrase "enterprise java beans" comes to mind, although I have never worked in that environment personally. – Pursuit Oct 26 '15 at 23:17
2

The simple way is to simply have a properties file, e.g., myapp.properties, with all your settings in. It's not a very advanced way to do settings but it suffices, or you can have your own XML based setup, or get them from a database, etc.

JeeBee
  • 17,476
  • 5
  • 50
  • 60