5

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

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I executed the below mentioned command but unable to read that file. java -classpath .;test.jar; -DPROP_FILE="C:\Program Files\DemoApp\config\project.properties" com.project.Main
It should be works fine. pls post your java code here.
3

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

Comments

1
java -classpath c:\java Client test.properties 

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.