How can I use jcommander to override the properties file value through command line arguments?

686 views
Skip to first unread message

compte...@gmail.com

unread,
Dec 22, 2014, 5:36:33 PM12/22/14
to jcomm...@googlegroups.com
I have a property file which is like this -

    hostName=machineA.domain.host.com
    emailFrom=tes...@host.com
    emailTo=wo...@host.com
    emailCc=he...@host.com
    
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";

        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?

How can I use jcommander here to override the properties file value through command line arguments and have --help option as well which can tell anyone who is trying to run the jar file through command line?

Niall Kelly

unread,
Jun 3, 2015, 9:18:19 AM6/3/15
to jcomm...@googlegroups.com
I had a similar requirement and found the following solution:

JCommander supports using the @ symbol to specify a parameter file (See http://jcommander.org/#Syntax)

So if I define params.conf as:
--option1 a
--option2 b

I can use the config file and override option1 by:

mytool @params.conf --option1 c

To allow this you have to explicitly allow parameter overwriting:

JCommander jCommander = new JCommander(configObj);
jCommander.setAllowParameterOverwriting(true);
jCommander.parse(args);



Reply all
Reply to author
Forward
0 new messages