Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about java properties file location

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?

like image 447
user613114 Avatar asked Feb 05 '12 16:02

user613114


People also ask

Where is Java properties file located?

properties file inside your resource folder of the java project. The extension we use for the properties file is . properties.

How do I put the file path in a property file?

when loading the properties from the file with the method Properties. load(), I need to use the escape character '\' in the path. So my path looks like: C:\\Users\\Harald\\Folder1\\Version1\\Folder2 . And it works this way, no exception is thrown.

What type of file is the properties file in Java?

properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.


2 Answers

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" 
like image 169
Perception Avatar answered Sep 19 '22 14:09

Perception


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.

like image 30
prajeesh kumar Avatar answered Sep 16 '22 14:09

prajeesh kumar