Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help understanding Command-Line Arguments

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Red face Help understanding Command-Line Arguments

    I am having trouble understanding what exactly Command-Line Arguments are. To my understanding they are arrays of the type String that is passed to the main method. In the program below, command-line information is displayed. When the program is executed, it displays contents in the “arg” array. Indexes 0, 1, and 2 are displayed with the elements “one”, “two”, and “three” also displayed in them respectively. Why is this? Also, the length instance variable is used determine the size of the “arg” array. Why is it three? Could someone answer these questions or also just explain to me how command-line arguments are used and what are the benefits for using them.


    // Display all command-line information. 
    class CLDemo {  
      public static void main(String args[]) { 
        System.out.println("There are " + args.length + 
                           " command-line arguments."); 
        System.out.println("They are: "); 
        for(int i=0; i<args.length; i++)  
          System.out.println("arg[" + i + "]: " + args[i]);  

    --- Update ---

    Sorry, I read my text book wrong and I now understand what my text book was getting at. There is no need to respond to this post.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Thanked 206 Times in 182 Posts

    Default Re: Help understanding Command-Line Arguments

    Yes, the args[] parameter is an array of strings that come from the command line. When you execute the java executable there will be

    (1) Some switches like -cp etc
    (2) Then the class name: in this case CLDemo
    (3) Then some other strings

    It's the ones in the last category that will be delivered to the main() method as the args parameter.

    The usefulness lies in the fact that the arguments can tell the CLDemo program what you want it to do. To take a non Java example you could issue the command:

    type CLDemo.java

    which will display the contents of the CLDemo.java file. It is necessary for the type command to be told the name of the file you wish to display! The way it finds out is by having access to the command line arguments. In this case there is one of them: the file to display.

    The reason why your program prints "arg[0]: foo" etc is that you have written a for loop that goes through the args array and prints the contents.


    I was too slow I'm glad you've got it sorted out!

Similar Threads

  1. How to check for shortage of command line arguments
    By IHeartProgramming in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 16th, 2012, 12:00 AM
  2. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 11:14 PM
  3. Multi-Valued Command Line Arguments
    By joey86 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 29th, 2009, 11:19 AM
  4. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM
  5. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM

Tags for this Thread