deepaksharma
deepaksharma

Reputation: 311

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

Upvotes: 5

Views: 23291

Answers (4)

Adriaan Koster
Adriaan Koster

Reputation: 16209

In your code load it as a java.util.ResourceBundle:

ResourceBundle properties = ResourceBundle.getBundle("project");

You can access the properties via the ResourceBundle API.

Put your properties file on the classpath when starting your app:

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

Upvotes: 1

sprabhakaran
sprabhakaran

Reputation: 1635

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.

Upvotes: 8

vishal
vishal

Reputation: 319

java -classpath c:\java Client test.properties 

"c:\java" is classpath - change to your java classpath

Upvotes: 1

Petr Mensik
Petr Mensik

Reputation: 27496

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);
 }

Upvotes: 3

Related Questions