Command Line Arguments Example Java Program

Definition

Command line arguments are optional string arguments that a user can give to a program upon execution. These arguments are passed by the operating system to the program, and the program can use them as input. Programs are normally run by invoking them by name.

Syntax

java Program_name Input_to_the_program

Command Line Arguments Example Program

class CommandLineArgumentsDemo{  
	public static void main(String args[]){  
	for(int i=0;i < args.length;i++){  
		System.out.println(args[i]);  
	}  	// Compile as javac CommandLineArgumentsDemo.java
}		// Run as java CommandLineArgumentsDemo She sells sea shells on the sea shore

Sample Output

Output is:
She
sells
sea
shells 
on 
the 
sea 
shore