The following code example shows how to Display Welcome Message Using Command Line Argument in Java.

Another program to show the usage of command line arguments is given below. It takes exactly one parameter. Otherwise, it displays the error and exits. Finally, we create the output by appending the parameter to the String “Welcome”.

public class Exercise2
{
	public static void main(String[] args) {
		String s1;
		if(args.length!=1)
		{
			System.out.println("Wrong Number of Command Line Arguments!\nExiting...");
			System.exit(0);
		}
		s1=args[0];
		
		System.out.println("Welcome "+s1);
	}
}

Output

F:\Java\JavaFundamentals\LanguageBasics>java Exercise2 Cherry
Welcome Cherry

F:\Java\JavaFundamentals\LanguageBasics>java Exercise2 Charlie
Welcome Charlie

A Program to Display Welcome Message Using Command Line Argument in Java
A Program to Display Welcome Message Using Command Line Argument in Java

Further Reading

30+ Basic Java Programs

Java Practice Exercise

programmingempire

Princites