How do I get command line arguments?

Category: fundamental, viewed: 9541 time(s).
package org.kodejava.examples.intro;

public class ArgumentParsingExample 
{
    public static void main(String[] args)
    {
        // When creating a Java application we will sometime need to pass a
        // couple parameters when the program is executed. To get the passed
        // parameter from the command line we can read the argument passed to
        // the main method just as the signature above. As you know to make a
        // class executable you need to create a method as follow:
        //
        // public static void main(String[] args) {}
        //
        // As you can see, this method take an array of String as the parameter.
        // You can guess that this array is the parameter that we passed in
        // the command line.
        for (int i = 0; i < args.length; i++)
        {
            System.out.println("Argument " + (i + 1) + " = " + args[i]);
        }
        
        // If you want to check if the number of supplied parameters meet the
        // program requirement you can check the size of the arguments array.
        if (args.length < 3)        
        {
            System.out.println("You must call the program as follow:");
            System.out.println("java ArgumentParsingExample arg1 arg2 arg3");
            
            // Exit from the program with an error status, for instance we
            // return -1 to indicate that this program exit abnormally
            System.exit(-1);
        }
        
        System.out.println("Hello, Welcome to ArgumentParsingExample Program");
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats