Command Line Arguments in Java with Example

What is Command Line Argument in Java?

Command Line Argument in Java is the information that is passed to the program when it is executed. The information passed is stored in the string array passed to the main() method and it is stored as a string. It is the information that directly follows the program’s name on the command line when it is running.

Example While running a class Demo, you can specify command line arguments as

java Demo arg1 arg2 arg3 …

Command Line Arguments in Java: Important Points

Example: To Learn java Command Line Arguments

Step 1) Copy the following code into an editor.

class Demo{
     public static void main(String b[]){
         System.out.println("Argument one = "+b[0]);
         System.out.println("Argument two = "+b[1]);
    }
}

Step 2) Save & Compile the code

Step 3) Run the code as java Demo apple orange. Step 4) You must get an output as below.

 

YOU MIGHT LIKE: