Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load config file (project.properties) at runtime via command prompt in java

I would like to load properties file via command prompt in java.

Properties file name: project.properties

java -classpath .;test.jar; com.project.Main

What 'll be the command if I'll load the properties files via command prompt.

Thank in advance.

I have executed the below mentioned command on command prompt but not get any output.

java -classpath .;test.jar; -DPROP_FILE="C:\Program Files\DemoApp\config\project.properties" com.project.Main

like image 999
deepaksharma Avatar asked Apr 03 '14 11:04

deepaksharma


2 Answers

Send file path as below format,

java -classpath .;test.jar; -DPROP_FILE=conf\project.properties com.project.Main

Use below code for getting property file

String propFile = System.getProperty("PROP_FILE");
Properties props = new Properties();
props.load(new FileInputStream(propFile));

Thanks.

like image 119
sprabhakaran Avatar answered Nov 05 '22 01:11

sprabhakaran


Run java -classpath .;test.jar; com.project.Main project.properties, than read this argument in your main method and load the file.

 public static void main(String[] args) {
     String fileName = args[0];
     Properties prop = new Properties();
     InputStream in = getClass().getResourceAsStream(fileName);
     prop.load(in);
 }
like image 41
Petr Mensik Avatar answered Nov 05 '22 01:11

Petr Mensik