Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java Command Line Arguments Tutorial

Command line arguments
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:
 
  1. public class CommandLineArguments {
  2.  public static void main(String args[]){
  3.  
  4.   /*
  5.    * To know the number of arguments passed to the program,
  6.    * use length property of argument array.
  7.    */
  8.  
  9.   int numberOfArguments = args.length;
  10.   System.out.println("There are " + numberOfArguments + " command line arguments");
  11.  
  12.   //print all arguments passed to the program.
  13.  
  14.   for(int i=0; i < numberOfArguments; i++)
  15.    System.out.println("Argument " + (i+1) + " is " + args[i]);
  16.  
  17.   /*
  18.    * You can also directly access the command line arguments as follow.
  19.    * args[0] will give first command line argument, args[1] will second
  20.    * and so on.
  21.    */
  22.  }
  23. }
Your rating: None Average: 3 (2 votes)
Share this

Post new comment

11 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.