1

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.

3
  • docs.oracle.com/javase/tutorial/essential/environment/… Commented Feb 2, 2018 at 20:56
  • 2
    You know that pesky entry point method your program's main class needs to have? The one with signature void main(String[] args)? The command-line arguments are provided in that array it receives as its parameter. Commented Feb 2, 2018 at 20:57
  • Can you provide us the code? It would be helpful. Commented Feb 2, 2018 at 21:27

1 Answer 1

4

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]);
    }
  }
}
Sign up to request clarification or add additional context in comments.

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.