Abbey Workshop

Java: Java Command Line Arguments

This short class demonstrates how command line arguments are passed in Java. Arguments are passed as a String array to the main method of a class. The first element (element 0) is the first argument passed not the name of the class.

   1:/** Class that demonstrates Java's command line arguments
   4:public class CmdLineArgs{
   5:    public static void main(String[] args){
   6:        System.out.println("The following command line arguments were passed:");
   8:        for (int i=0; i < args.length; i++){
   9:            System.out.println("arg[" + i + "]: " + args[i]);
  10:        }
  11:    }

With the following command line, the output shown is produced.

java CmdLineArgs zero one two three


The following command line arguments were passed:
arg[0]: zero
arg[1]: one
arg[2]: two
arg[3]: three