Reading from External Properties in Java

Ameya Dhamnaskar
2 min readOct 26, 2018

--

After reading through a lot of resources and multiple failed attempts to configure my java code to fetch configuration properties from an external folder I have finally found a way which works.Making config.properties file external helps a lot when you are expected to set up different variables for different environment ,example : separate values of for variables development / production environment .These are my findings and hope they help you out :

Inside config.properties each parameter is stored as a pair of strings, one storing the name of the parameter (called the key), and the other storing the value.Your Config File is expected to look like:

config.propertiesName=AmeyaD
Execute_Time=1200
Location=/Users/ABC/Work/file.txt

we are trying to use the values stored in the config.properties/dev.properties/prod.properties inside our java code which is present as a jar file.Packaging for my Application looks like this and we are trying to access the properties inside config folder of your application:

Server(Your Application Name)
+- bin
+- run.sh
+- config
+- config.properties
+- dev.properties
+- prod.properties
+- lib
+- server-0.0.1.jar

To access these external properties we need to make certain changes to our Java code .Example of how the Java Code should look like :

package org.apache.coms;
public class HTTPMain
{
public static void main (String[] args)
{
String serverdir = System.getProperty("server.home");

// by default set to dev but env has to be set while running java jar file.
String env = System.getProperty("env", "dev");
String config = env + ".properites";
System.out.println("config filename is : " + config);
Properties prop = new Properties();
InputStream inStream = null;
try {
inStream = getClass().getClassLoader().getResourceAsStream(config);
props.load(inStream);
} finally {
if (inStream != null)
inStream.close();
}
String name=prop.getProperty("Name");
String Execute_Time=prop.getProperty("Execute_Time");
int execute_time=Integer.parseInt(Execute_Time);
String Location=prop.getProperty("Location");
}
}

Finally a Bash Script to start your Application in Production mode with prod.properties.We run our run.sh bash script which is present in the bin folder of our application :

#run.sh
#/Path_to_Server_Directory/bin bash
Server_Dir=$(cd "`dirname $0`/.." && pwd)
java -Denv=prod -Dserver.home=$Server_Dir -cp "$Server_Dir/config:$Server_Dir/lib/*" org.apache.coms.HTTPMain "$@"

In the above script :
* Run Script using :

bash > Path_To_Server_Directory/bin/run.sh

* Replace the org.apache.coms.HTTPMain with your package_name.(Name of your Main Class).
* Replace prod with the name of .properties file you are using it will be reflected in the code.ex : dev.properties/config.properties
* If your jar file is outside lib just remove lib from class path :
java -Dserver.home=$Server_Dir -cp "$PATH_TO_DIR/config:$Server_Dir/*"

Hope this works for you.Feel free to reach out in any case of discrepancies or doubt.

--

--