Search code examples
javacommand-line

How to get input from command line argument


currently my file is run by me calling Java Server and then entering a port number after the program has started like this:

java Server

Port #:

I do this by creating a scanner in my main and just printing the prompt and waiting for input. and I can enter 7777 to set the port to 7777.

but how do I make it so I can just enter the port number like this?

java Server 7777

I tried it on my current code but the number entered after server just doesn't do anything and I'll still have to enter it into the prompt after.


Solution

  • This simple example from this tutorial page should help you.

    class cmd
    {
      public static void main(String[] args)
      {
        for(int i=0;i< args.length;i++)
        {
        System.out.println(args[i]);
        }
      }
    }