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?