Results 1 to 7 of 7
  1. #1
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default Command Line Argument

    I can read command line arguments as a String by writing 'args[]' only in main class. However, it fails when I enter methods in different classes.

    Your help is appreciated.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default huh?

    Sorry, but i didn't quite understand your question/problem...
    • You want to pass the args array to other methods?
    • you want to use the args array in other classes?

    Please provide some code to understand.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Yes, are you talking about classes or methods? If classes, make a public field (or private with a public accessor method) and then other classes can use it. The field should be a String[], and in the main method you should write "argField = args;", argField being the name of the field. This is assuming that you are running the class that asks for the arguments.
    If methods, make a field, and then each method can use it. Initialize the field in main method same way as the above.
    Hope this helped
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  4. #4
    right2001 is offline Member
    Join Date
    Feb 2009
    Posts
    25
    Rep Power
    0

    Default I think that would work

    Do you mean that "public static void main (final String[] args)"?
    when i did that the bug disappear. I am not sure whether it can be used to read command line arguments.

    Thank you in advance.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Examples, anyone?

    MK12... you lost me. Could you please provide some examples?
    right2001... if you have any doubts about how to do something, try it out.
    when i did that the bug disappear
    • what bug were you seeing?
    • Can you post some to understand your problem?

    A couple of links that might help:
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    I think he meant passing the command args to a different method. If that was it, use a String[] argument in the method that needs them, and when you call it, pass the args String[].
    Same thing could work if method was in a different class.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  7. #7
    saurabh1073 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    If you like to pass the command line arguments to some other methods then it is simple. use in this manner
    class A{

    void get(String a[])
    {

    }
    public static void main(String f[])
    {
    new A().get(f);
    }

    }

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 11:37 PM
  2. command line args
    By MarkWilson in forum NetBeans
    Replies: 3
    Last Post: 08-04-2008, 03:22 AM
  3. Java command line
    By agouzoul in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 12:12 PM
  4. Exporting from the command line
    By o1121 in forum Eclipse
    Replies: 1
    Last Post: 08-09-2007, 07:29 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 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
  •