Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the properties file value through command line arguments?

I have a property file which is like this -

hostName=machineA.domain.host.com
[email protected]
[email protected]
[email protected]

And now I am reading the above property file from my Java program as -

public class FileReaderTask {
    private static String hostName;
    private static String emailFrom;
    private static String emailTo;
    private static String emailCc;

    private static final String configFileName = "config.properties";
    private static final Properties prop = new Properties();

    public static void main(String[] args) {
        readConfig(arguments);
    }

    private static void readConfig(String[] args) throws FileNotFoundException, IOException {
        if (!TestUtils.isEmpty(args) && args.length != 0) {
            prop.load(new FileInputStream(args[0]));
        } else {
            prop.load(FileReaderTask.class.getClassLoader().getResourceAsStream(configFileName));
        }

        hostName = prop.getProperty("hostName").trim();         
        emailFrom = prop.getProperty("emailFrom").trim();
        emailTo = prop.getProperty("emailTo").trim();
        emailCc = prop.getProperty("emailCc").trim();
    }
}

Most of the time, I will be running my above program through command line as a runnable jar like this -

java -jar abc.jar config.properties

My questions are -

  • Is there any way we can override the above attributes in the property file through command line without touching the file if needed? Since I don't want to modify my config.properties file always whenever I need to change any attributes value? Is this possible to do?

Something like this should override the hostName value in the file?

java -jar abc.jar config.properties hostName=machineB.domain.host.com
  • And also, is there any way to add --help while running the abc.jar that can tell us more about how to run the jar file and what does each property means and how to use them? I have seen --help while running most of the C++ executable or Unix stuff so not sure how we can do the same thing in Java?

Do I need to use CommandLine parser for this in Java to achieve both of the things?

like image 917
john Avatar asked Dec 22 '14 22:12

john


People also ask

How do you pass a file to a command line argument in Java?

public static void main(String[] args) { fileReader fr = new fileReader(); getList lists = new getList(); File CP_file = new File("C:/Users/XYZ/workspace/Customer_Product_info. txt"); int count = fr. fileSizeInLines(CP_file); System. out.

In which method we can pass command line arguments?

Internally, JVM wraps up these command-line arguments into the args[ ] array that we pass into the main() function. We can check these arguments using args. length method. JVM stores the first command-line argument at args[0], the second at args[1], the third at args[2], and so on.


1 Answers

If the only things you will have on your commandline are things like: hostName=machineB.domain.host.com and not any other types of arguments, then you can simplify your commandline handling quite a lot:

First, join all the command-line args with newlines as if they were a new config file:

StringBuilder sb = new StringBuilder();
for (String arg : args) {
    sb.append(arg).append("\n");
}
String commandlineProperties = sb.toString();

now, you have two property sources, your file, and this string. You can load them both in to a single Properties instance, with one version overwriting the other:

if (...the config file exists...) {
    try (FileReader fromFile = new FileReader("config.properties")) {
        prop.load(fromFile);
    }
}

if (!commandlineProperties.isEmpty()) {
    // read, and overwrite, properties from the commandline...
    prop.load(new StringReader(commandlineProperties));
}
like image 173
rolfl Avatar answered Oct 27 '22 02:10

rolfl