-1

I would like to have a dynamic way of passing in parameters to a java main method call which is done via the Command Line(cmd) to a Runnable JAR file. At the moment my main() method takes 6 parameters and sets each one to a variable before calling another method with those variables passed in.

What id like is a way to give a user the ability to pass 5 or less paramters to the command line and have it safely handle the missed parameter by setting it to null or an empty string("") value.

For example, if I ran the command below, it should know to set the missing parameters I have not specified(clientName and outputFolder), to an empty String.

java -Xmx1024m -jar MainApp.jar "Summary" **<missing>** "2015-06-07" "https://12345.bp.com/bp/" "c:\\Parameters.txt" **<missing>**

Here is the code I have for my main method:

public static void main(String[] args) {
        try {               
            String dType = args[0];
            String clientName = args[1];
            String cycleString = args[2];
            String mspsURL = args[3];
            String inputFile = args[4];
            String outputFolder = args[5];

            System.out.println("**Main Parameters passed**");
            for(String x : args) {
                System.out.println(x);
            }

            runLogic(dType, clientName, cycleString, 
                    mspsURL, inputFile, outputFolder);          
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

Any help appreciated.

MisterIbbs
  • 247
  • 1
  • 7
  • 20
  • Do you mean that the user should type `****` into the command line? or do you mean that the argument is actually missing from the command? – khelwood Jun 10 '15 at 15:44
  • I was hoping there would be a way for the method to know if the parameter was missing from the command. But not sure if thats possible. So yes, the user would have a space or a character there to denote a missing field. – MisterIbbs Jun 10 '15 at 15:48
  • In that case, just have the user enter `""` or `" "` in missing spaces – River Jun 10 '15 at 15:50
  • I added the case for you being able to input a keyword to signify a missing argument – River Jun 10 '15 at 15:52
  • Yes that would allow me to handle the data by checking the character. I am aiming to reduce the complexity of this command by giving the user a less fixed command. – MisterIbbs Jun 10 '15 at 15:55
  • I feel the keyword approach is closer to what you were attempting initially, and works if you would like to preserve order. – River Jun 10 '15 at 15:56

2 Answers2

4

Suppose for example that last two are optional.

String dType = args[0];
String clientName = args[1];
String cycleString = args[2];
String mspsURL = args[3];
String inputFile = (args.length < 4 ? "default inputFile" : args[4]);
String outputFolder = (args.length < 5 ? "default outputFolder" : args[5]);
Zereges
  • 5,139
  • 1
  • 25
  • 49
  • Ah that will work, i can move the parameters that can be missed to the end of the command line entry. Thanks for this. – MisterIbbs Jun 10 '15 at 15:52
1

If what you really want is a keyword for missing items, you could do:

String dType = (args[0].equals(keyword)?"":arg[0]);
String clientName = (args[1].equals(keyword)?"":arg[1]);
String cycleString = (args[2].equals(keyword)?"":arg[2]);
String mspsURL = (args[3].equals(keyword)?"":arg[3]);
String inputFile = (args[4].equals(keyword)?"":arg[4]);
String outputFolder = (args[5].equals(keyword)?"":arg[5]);

Then your input would be:

java -Xmx1024m -jar MainApp.jar "Summary" "keyword" "2015-06-07" "https://12345.bp.com/bp/" "c:\\Parameters.txt" "keyword"

Otherwise I would use the length attribute of the Array class.

Test for how long the input array is and only assign values for that many variables.

Example:

int i = args.length;
String dType = (i>0?args[0]:""); //Just do this for each argument
i--; //Then reduce counter by 1
River
  • 8,585
  • 14
  • 54
  • 67
  • When someone does not pass at least 6 arguments to the command line, you will get `ArrayIndexOutOfBoundsException` when trying to access `args` with `index >= args.length - 1` – Zereges Jun 10 '15 at 15:57
  • With which approach? The top assumes "keyword" is replaced in all missing slots as in the OP's original post. (`****` is the keyword there) – River Jun 10 '15 at 15:58
  • Sorry, I thought OP wants to remove parameter by not specifying it, not by inserting some dummy value. – Zereges Jun 10 '15 at 16:00
  • That could be achieved using the second method – River Jun 10 '15 at 16:04