Results 1 to 7 of 7
  1. #1
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Passing values to main(String args[]) ?

    Hi, I saw some code which appeared to pass values to void main(String args[])
    and I was wondering how this is done and why it would be done.

    example:

    Java Code:
     public static void main(String args[]) {
        if (args.length < 2) {
          System.out.println("some message");
        }
    Also, where exactly in code do you call the void main(String args[]) function. :confused: I have never seen anything like for example:

    Java Code:
    String str="hello";
    if(someCondition==true)
    {
    main(hello);
    }
    Thanks in advance for any help :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you have a program Hello.java which you compile to Hello.class, you pass these parameters (known as command line parameters) when you call the program:

    c:\java>java Hello this is a command line parameter

    java Hello is calling your program and all that follows are the command line parameters.

  3. #3
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    when you can any java file from command line you can pass arguments. If length of the command line arguments is less than 2 then it will print that message.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    If you have a program Hello.java which you compile to Hello.class, you pass these parameters (known as command line parameters) when you call the program:

    c:\java>java Hello this is a command line parameter

    java Hello is calling your program and all that follows are the command line parameters.
    Ok so let me see if I understand. If I were to write c:\java>java Hello
    then args[0] would contain nothing?

    but if I were to write c:\java>java Hello msg
    then args[0] would be msg.

    I have just used run configuration in eclipse and used (x)=Arguments option and wrote something in there and ran the program and args.length printed 1. Is this the same idea as the command line parameter pass?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    I have just used run configuration in eclipse and used (x)=Arguments option and wrote something in there and ran the program and args.length printed 1. Is this the same idea as the command line parameter pass?
    Yes indeed.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Thanks Fubarable.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Ok so let me see if I understand. If I were to write c:\java>java Hello
    then args[0] would contain nothing?

    but if I were to write c:\java>java Hello msg
    then args[0] would be msg.

    I have just used run configuration in eclipse and used (x)=Arguments option and wrote something in there and ran the program and args.length printed 1. Is this the same idea as the command line parameter pass?
    Hallo Marshy

    if you don't pass any parameters and you ask for args[0] then you will get a OutOfBoundsExeption. So, to avoid this, use this for-example

    Java Code:
    		for (int i = 0; i < args.length; i++) {
    			System.out.println(args[i]);
    		}
    Even if no parameter are passed no exception will be thrown and you don't have to care how many parameters are passed, because all will be processed in the for-loop.

Similar Threads

  1. Why can't we write main without String args[]
    By shailender in forum New To Java
    Replies: 6
    Last Post: 06-17-2013, 10:28 AM
  2. String, quotes and args
    By mac in forum New To Java
    Replies: 8
    Last Post: 02-04-2010, 05:24 PM
  3. Public static void main (String args[])
    By arefeh in forum New To Java
    Replies: 12
    Last Post: 01-28-2010, 12:58 PM
  4. passing values from main page to pop up window
    By vicky in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-29-2008, 04:06 PM
  5. Replies: 9
    Last Post: 11-23-2008, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •