Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
All java applications contain a static method called main. Signature of the main method is as given below
public static void main(String[] args)
Main method has one argument of type array of String objects. This array represents any arguments which may have been passed to the application while invoking it using command line. These arguments are called command line arguments.
Command line arguments are mainly used to pass certain configuration parameters which can be used by the application at runtime.
Command line arguments can be retrieved in the program as given below.
Command line arguments example:
- public class CommandLineArguments {
- public static void main(String args[]){
- /*
- * To know the number of arguments passed to the program,
- * use length property of argument array.
- */
- int numberOfArguments = args.length;
- System.out.println("There are " + numberOfArguments + " command line arguments");
- //print all arguments passed to the program.
- for(int i=0; i < numberOfArguments; i++)
- System.out.println("Argument " + (i+1) + " is " + args[i]);
- /*
- * You can also directly access the command line arguments as follow.
- * args[0] will give first command line argument, args[1] will second
- * and so on.
- */
- }
- }

Post new comment