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 -
Something like this should override the hostName value in the file?
java -jar abc.jar config.properties hostName=machineB.domain.host.com
--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?
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.
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.
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With