1

I have a requirement to parse some command line arguments in a Java application without using any third party libraries (like Apache CLI or JCommander). I can only use the standard Java 11 API.

They will be passed into my application on start up and with flags; e.g: java myClass -port 14008 -ip 140.086.12.12 which can be in any order.

I'm pretty new to Java, but how would I go about doing this. Can any standard API functions help with this? Or should I check them manually e.g: if arg[0] = "-port" then arg[0]+1 = port number.

3
  • what is your input and expected output? Commented Mar 25, 2022 at 10:14
  • Hi, Input is above, so Java myClass -port 14008 -ip 140.086.12.12 . Output would be 2 variables with the port and ip address in them. Commented Mar 25, 2022 at 18:59
  • "without using any third party libraries" consider why these libraries exist: there would be no need for them if there was a simple solution. Sure, you can do it without, but you will end up with something less functional; you need to decide what functionality you are willing to forgo. Commented Apr 2, 2022 at 13:00

2 Answers 2

1

The command line will split with white spaces everything you'll write after your "Java MyClass". So, every "word" separated with a space will be treated as a single parameter.

You might wanna pack them together and use a separator character to split them later, like so Java myClass -port:14008 -ip:140.086.12.12.

Or, maybe if you wanna keep your parameter style you need to add the index displacement within brackets, like this

int port = 0;
String ip = "";
for (int i=0; i<args.length; i+=2){
    if (args[i].equals("port")){
       port = Integer.parseInt(args[i+1]);
    } else if (args[i].equals("ip")){
       ip = args[i+1];
    }
}

By the way, this is a very good article that explains quite quickly how Java accpets command line parameters. You might find it useful.

https://www.baeldung.com/java-command-line-arguments

EDIT: thanks to @Andy Turner in the comments

Sign up to request clarification or add additional context in comments.

2 Comments

"Java will split with white spaces" no, the shell will do this.
@AndyTurner you're absolutely right. I'll edit it!
1

This is how I solved the problem. It assumes there are class member variables set with default values to overide.

private String ipAddress = "localhost";

private Integer port = 14001;

     /**
     * Method to get the ip and port number from the command line args.
     * It caters for any order of flag entry.It tries to override the
     * defaults set as a class variables, so if it can't, it uses those.
     *
     * @param args The args passed in by the user.
     */
    private void getPortAndIp(String[] args) {
        System.out.println("Getting inputs ");
        if ((args[0].equals("-ccp")) && (args.length == 2)) {
            this.port = Integer.parseInt(args[1]);
        } else if ((args[0].equals("-cca")) && (args.length == 2)) {
            this.ipAddress = args[1];
        } else if ((args[0].equals("-ccp")) && (args[2].equals("-cca"))
                && (args.length == 4)) {
            this.port = Integer.parseInt(args[1]);
            this.ipAddress = args[3];
        } else if ((args[0].equals("-cca")) && (args[2].equals("-ccp"))
                && (args.length == 4)) {
            this.port = Integer.parseInt(args[3]);
            this.ipAddress = args[1];
        } else {
            System.out.println("Options:");
            System.out.println("-ccp [port number]");
            System.out.println("-cca [ip address]");
            System.out.println("Could not determine port from command line " +
                    "arguments, using defaults: ");
        }
        System.out.println("on " + this.ipAddress + ":" + this.port);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.