Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
153 views
Welcome To Ask or Share your Answers For Others

1 Answer

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]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...