Showing posts with label Command line arguments. Show all posts
Showing posts with label Command line arguments. Show all posts

Sunday, 23 June 2019

Batch: Command line arguments


Command line arguments are passed to the batch command, based on the arguments passed, batch command takes specific action.

For example, when you execute the command ‘dir’, it lists all the files in a directory.

C:\Users\Public\databases>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 1034-4F6F

 Directory of C:\Users\Public\databases

08/07/2018  09:28 PM    <DIR>          .
08/07/2018  09:28 PM    <DIR>          ..
08/07/2018  09:35 PM    <DIR>          batch script tutorial
02/10/2017  09:17 AM               436 customerDb.properties
02/10/2017  09:17 AM        52,887,096 customerDb.script
08/07/2018  09:28 PM    <DIR>          testApp
               2 File(s)     52,887,532 bytes
               4 Dir(s)  24,016,216,064 bytes free


You can lists all the directories in a specific directory by passing the directory name as command line argument.

C:\Users\Public\databases>dir testApp
 Volume in drive C is OSDisk
 Volume Serial Number is 1034-4F6F

 Directory of C:\Users\Public\databases\testApp

08/07/2018  09:28 PM    <DIR>          .
08/07/2018  09:28 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  24,014,905,344 bytes free

C:\Users\Public\databases>



Previous                                                 Next                                                 Home

Saturday, 15 February 2014

Command Line Arguments

Java provides an option like, a java application can accept any number of arguments from command line. This option facilitates us to configure Application at the time of running.

User should pass command line arguments, at the time of launching application.

Syntax
   java ClassName arguments

When an application launched, The command line arguments are copied to the String argument of the main method.
Example 1
class CommandLine{
  public static void main(String args[]){
    System.out.println("Number of arguments passes is " + args.length);
  
    for(int i=0; i < args.length; i++){
      System.out.println(args[i]);
    }
  }
}

Run the Application like below
   java CommandLine Hi How Are You

Output
Number of arguments passes is 4
Hi
How
Are
You

As you see args[0] contains String “Hi” args[1] contains “How” etc.,

Example 2
class CommandLine{
  public static void main(String args[]){
    if(args[0].equals("Day"))
      System.out.println("Good Morning, Have a Great Day");

    if(args[0].equals("Night"))
      System.out.println("Sleep Well, Good Night");
  }
}

Run 1
java CommandLine Day
Good Morning, Have a Great Day

Run 2
java CommandLine Night
Sleep Well, Good Night

Some Points to Remember
1. If no command line arguments are provided then, length of args is zero
class CommandLine{
  public static void main(String args[]){
    System.out.println(args.length);
  }
}
 

Output
0    
        
2. If there is no command line arguments, then the String array of Main method will be empty or null?
It is Empty,not null.


Main method                                                 enum types                                                 Home