COMMAND LINE ARGUMENTS

Command Line Arguments


    Command line arguments are the arguments which are passed at the run time during program execution,

    Example:  While running a class CommandDemo ,you can specify command line arguments as
           java CommandDemo arg1,arg2,arg3…………..
  • We can have N no.of command line arguments.

  • Information is passed as Strings.

  • They are captured into the String argument of your main method.

  • Space is the delimiter between command line arguments


  • The below example describes command line arguments:

    Example 1:

     
                    
                 package com.sdj;
                 public class CommandDemo 
                 {
                    public static void main(String args[ ]) 
                    {
                       System.out.println("The received argument is:"+args[0]);
                    }
                 }                

       Output:

       E:\lab\1stbatch\src>javac CommandDemo.java
       E:\lab\1stbatch\classes>java CommandDemo
       Here we will receive because args[0] expecting one
       input parameter but we did not provide it.
       E:\lab\1stbatch\classes>java CommandDemo “Ravi.p”
       The received argument is:Ravi.p

    The below example illustratesthe way of passing multiple arguments:

    Example 2:

     
                    
                 package com.sdj;
                 public class CommandDemo 
                 {
                    public static void main(String args[]) 
                    {
                       System.out.println("The received first argument is:"+args[0]);
                       System.out.println("The received second argument is:"+args[1]);
                       System.out.println("The received third argument is:"+args[2]);
                    }
                 }                

       Output:

       E:\lab\1stbatch\src>javac CommandDemo.java
       E:\lab\1stbatch\classes>java CommandDemo “SDJ” “INFO” “SOFT “
       The received first argument is:SDJ
       The received second argument is:INFO
       The received third argument is:SOFT

      Note

      Space is the delimiter between one cmd argmt to another cmd line arguments.

  • We can pass space as cmdline arguments at a time by using “ ” .

  • Example 3:

     
                    
                 package com.sdj;
                 public class CommandDemo
                 {
                    public static void main(String args[]) 
                    {
                       System.out.println("The received first argument is:"+args[0]);   
                    }
                 }                

       Output:

       E:\lab\1stbatch\src>javac CommandDemo.java
       E:\lab\1stbatch\classes>java CommandDemo “Praveen Sujata Pratibha Anwesha Prem Asha”
       The received first argument is: Praveen Sujata Pratibha Anwesha Prem Asha

    Limitations of commandlinearguments:

    • In command line argument we have one major drawback i.e. User should knows the No.of cmd line arguments and order of cmd Line arguments which are expecting in the program.

      For Example:
        If have code like below inside main method
        main(String args[]) {
          String name = args[0];
          int age = Integer.parseInt(args[1]);
        }

    • In order to make compile success the above program first argument should be String type And second argument should be integer type.

        Java ClassName “SDJ” 100 we should give inputs in this formats only .
        Atleast if u change order of arguments like 100 “SDJ” then the program won’t run successfully.
    • In order to overcome drawbacks of command line arguments better to go through the System Properties.