I have a program that will read everything from a file config.propertiesif there are no arguments on the command line other than the location of the config.properties file. Below is my config.properties file -
NUMBER_OF_THREADS: 100
NUMBER_OF_TASKS: 10000
ID_START_RANGE: 1
TABLES: TABLE1,TABLE2
If I run my program from the command line, for example:
java -jar Test.jar "C:\\test\\config.properties"
It should read all four properties from the file config.properties. But suppose if I run my program like this:
java -jar Test.jar "C:\\test\\config.properties" 10 100 2 TABLE1 TABLE2 TABLE3
then it should read all the properties from the arguments and overwrite the properties in the config.properties file.
Below is my code that works great in this scenario -
public static void main(String[] args) {
try {
readPropertyFiles(args);
} catch (Exception e) {
LOG.error("Threw a Exception in" + CNAME + e);
}
}
private static void readPropertyFiles(String[] args) throws FileNotFoundException, IOException {
location = args[0];
prop.load(new FileInputStream(location));
if(args.length >= 1) {
noOfThreads = Integer.parseInt(args[1]);
noOfTasks = Integer.parseInt(args[2]);
startRange = Integer.parseInt(args[3]);
tableName = new String[args.length - 4];
for (int i = 0; i < tableName.length; i++) {
tableName[i] = args[i + 4];
tableNames.add(tableName[i]);
}
} else {
noOfThreads = Integer.parseInt(prop.getProperty("NUMBER_OF_THREADS").trim());
noOfTasks = Integer.parseInt(prop.getProperty("NUMBER_OF_TASKS").trim());
startRange = Integer.parseInt(prop.getProperty("ID_START_RANGE").trim());
tableNames = Arrays.asList(prop.getProperty("TABLES").trim().split(","));
}
for (String arg : tableNames) {
}
}
Problem: -
Now what I'm trying to do is suppose someone starts a program like this
java -jar Test.jar "C:\\test\\config.properties" 10
noOfThreads -
noOfThreads should be 10 instead of 100
-
java -jar Test.jar "C:\\test\\config.properties" 10 100
noOfThreads noOfTasks -
noOfThreads should be 10 instead of 100
noOfTasks should be 100 instead of 10000
.
- , ?