27

I have simple java project with structure:

package com.abc: a.java b.java c.properties

I have database configuration parameters configured in c.properties file. Inside a.java and b.java, I am loading properties file using:

Properties p = new Properties();
InputStream in = this.getClass().getResourceAsStream("c.properties");
p.load(in);

This works fine. But the main question is, once I prepare executable jar by exporting this code, properties file also gets packaged in jar file. If someone else wants to modify properties file for different database configuration, how can he do it? Do I have to store properties file in some fixed location in local machine. e.g. "c:/". Then give jar along with properties file to the other person. Then he needs to copy properties file inside C:/ location? Also one more question, how can i make this location generic for windows and linux machine?

user613114
  • 2,731
  • 11
  • 47
  • 73
  • possible duplicate of [Load files External To The Distribution Jar](http://stackoverflow.com/questions/9142571/load-files-external-to-the-distribution-jar) – Brian Roach Feb 05 '12 at 16:46

4 Answers4

33

The typical way of handling this is to load the base properties from your embedded file, and allow users of the application to specify an additional file with overrides. Some pseudocode:

Properties p = new Properties();
InputStream in = this.getClass().getResourceAsStream("c.properties");
p.load(in);

String externalFileName = System.getProperty("app.properties");
InputStream fin = new FileInputStream(new File(externalFileName));
p.load(fin);

Your program would be invoked similar to this:

java -jar app.jar -Dapp.properties="/path/to/custom/app.properties"
Perception
  • 79,279
  • 19
  • 185
  • 195
  • This seems to be the perfect solution. I will surely try this. Thanks. – user613114 Feb 05 '12 at 17:02
  • You could also let the user put a file with the same name and path as in the jar file in a directory that is also in the classpath, before the jar. In this case, the file from the directory would supersede the file in the jar file. – JB Nizet Feb 05 '12 at 17:09
3

First keep the default properties in your properties file, which gets packed into the jar. When the application starts try reading a same named properties file from some default location in filesystem, preferrable the user's home folder which you can obtain by System.getProperty("user.home");. If the file exists at the filesystem load it, if it doesn't exist then load your packed properties file and write a copy to the filesystem.

So if your properties file name is myprops.properties, initially only your jar file will contain it. When the application starts up it will check whether /home/xyz/myprops.properties file exists. Since it doesn't, it will read the packed properties file and write a copy to /home/xyz/myprops.properties file. From next time onwards, it will read from /home/xyz/myprops.properties.

Sampada
  • 2,931
  • 7
  • 27
  • 39
prajeesh kumar
  • 1,916
  • 1
  • 17
  • 17
2

Why not pass the location of the properties file as a command line argument (following a flag)? if it's not present, then use the default one in the jar file.

msgmash.com
  • 1,035
  • 5
  • 10
0

You're loading the properties file from the class path. I'd suggest something like this:

Properties location

Community
  • 1
  • 1
Jeremy Ross
  • 11,544
  • 3
  • 36
  • 36