How to pass arguments to main method in Java?

To pass arguments to main method in Java, add them to the java command like so: java MainClass arg1 arg2 arg3.

For example, this java command:

java MainClass Alice Bob Carol

Will pass three arguments to MainClass:

public class MainClass {

  public static void main(String[] args) {
    System.out.println(args.length); // 3
    System.out.println(args[0]); // Alice
    System.out.println(args[1]); // Bob
    System.out.println(args[2]); // Carol
  }
}