Java command line argument program

Java command line argument

Java command line argument program

A command-line argument is information that directly follows the program’s name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy. They are stored as strings in the String array passed to main( ).
If we run a Java Program by writing the command “Welcome to edunews.tech” where the name of the class is “EduNews”, then it will run upto EduNews.

// Program to check for command line arguments 
class EduNews 
{ 
    public static void main(String[] args) 
    { 
        // check if length of args array is 
        // greater than 0 
        if (args.length > 0) 
        { 
            System.out.println("The command line"+ 
                               " arguments are:"); 
  
            // iterating the args array and printing 
            // the command line arguments 
            for (String val:args) 
                System.out.println(val); 
        } 
        else
            System.out.println("No command line "+ 
                               "arguments found."); 
    } 
}

Try executing this program as shown here –

$java EduNews Welcome to edunews.tech

Program Output:

The command line arguments are:
Welcome
to
edunews.tech

I hope this post helps you to understand the Command line argument and its implementation in Java programming language.

Keep coding 🙂

2 thoughts on “Java command line argument program”

Leave a Reply

Your email address will not be published. Required fields are marked *