Pass arguments as name value pairs to java program

Discussion in 'Java' started by dufffman@gmail.com, Jun 6, 2006.

  1. Guest

    Hi,

    I have seen java programs executed as below..

    java TestArguementProgram --arg1 firstParameter --arg2 secondParameter

    My question is, how is this format parsed once inside the java program
    (besides using StringTokenizer)? I have used the Properties class to
    parse name value pairs from a file, but figured that since its such
    common convention, there has to be a better approach in java.

    Thanks,
    , Jun 6, 2006
    #1
    1. Advertising

  2. <> wrote:
    > I have seen java programs executed as below..
    >
    > java TestArguementProgram --arg1 firstParameter --arg2 secondParameter
    >
    > My question is, how is this format parsed once inside the java program
    > (besides using StringTokenizer)? I have used the Properties class to
    > parse name value pairs from a file, but figured that since its such
    > common convention, there has to be a better approach in java.


    The JVM does the job of tokenizing (breaking the command line into separate
    strings) for you.
    Suppose your code looks like this:
    public class TestArguementProgram {
    public static void main(String args[]) {
    ...
    }
    }
    Then, in your example, the JVM calls your main method with the following
    String array argument:
    { "--arg1", "firstParameter", "--arg2", "secondParameter" }
    A simple idiom to process such argument arrays is:

    public static void main(String args[]) {
    String arg1 = null;
    String arg2 = null;
    for (int i = 0; i < args.length; i++) {
    if (args.equals("--arg1")
    arg1 = args[i++];
    else if (args.equals("--arg2")
    arg2 = args[i++];
    }
    ... // do more things
    }

    In real life you will have to add some error checking to cope with malicious
    command lines.

    --
    Thomas
    Thomas Fritsch, Jun 6, 2006
    #2
    1. Advertising

  3. "Thomas Fritsch" <> wrote:
    > A simple idiom to process such argument arrays is:
    >
    > public static void main(String args[]) {
    > String arg1 = null;
    > String arg2 = null;
    > for (int i = 0; i < args.length; i++) {
    > if (args.equals("--arg1")
    > arg1 = args[i++];

    Oops, the above is wrong. It should be:
    arg1 = args[++i];
    > else if (args.equals("--arg2")
    > arg2 = args[i++];

    arg2 = args[++i];
    > }
    > ... // do more things
    > }


    --
    Thomas
    Thomas Fritsch, Jun 7, 2006
    #3
  4. wrote:
    > Hi,
    >
    > I have seen java programs executed as below..
    >
    > java TestArguementProgram --arg1 firstParameter --arg2 secondParameter
    >
    > My question is, how is this format parsed once inside the java program
    > (besides using StringTokenizer)? I have used the Properties class to
    > parse name value pairs from a file, but figured that since its such
    > common convention, there has to be a better approach in java.
    >
    > Thanks,


    A related question, has anyone else tried to pass UTF-8 (or other
    Unicode) to java program via proram arguments. Before entering
    main(String args[]) JVM likely uses system's default character encoding
    to make each member of args.

    I encountered this situation, where I wanted to invoke a Java program
    from a C program and pass UTF-8 data. I considered retrieving bytes
    from String instances passed to main and then creating String instances
    with UTF-8 encoding, but decided that I did not know the details of
    String's internals well enough to feel confident I was going to get
    bytes out exactly. So I encoded each UTF-8 character in something I
    knew would be passed correctly and subsequently decoded on Java side.

    Any suggestions?

    All the best,
    Opalinski

    http://www.geocities.com/opalpaweb/
    opalinski from opalpaweb, Jun 7, 2006
    #4
  5. Chris Uppal Guest

    wrote:

    > A related question, has anyone else tried to pass UTF-8 (or other
    > Unicode) to java program via proram arguments. Before entering
    > main(String args[]) JVM likely uses system's default character encoding
    > to make each member of args.


    According to the source to the launcher, it interprets the argument strings as
    byte arrays encoded using the value of the system property "sun.jnu.encoding".
    I don't think that's /exactly/ the same as system default encoding
    (Charset.defaultCharset() returns the value of the "file.encoding" property),
    though I imagine they will usually coincide in practice. That property seems
    to be used for general JNI-related things (including AWT), so, althought I
    suppose you /could/ use:
    java -Dsun.jnu.encoding=UTF-8 ...
    somehow I don't think it would be a very good idea ;-)


    > So I encoded each UTF-8 character in something I
    > knew would be passed correctly and subsequently decoded on Java side.



    Sounds like the right way to do it. Especially as not all systems make it easy
    to enter UTF-8 on the command line.

    -- chris
    Chris Uppal, Jun 7, 2006
    #5
  6. sez:
    > Hi,
    >
    > I have seen java programs executed as below..
    >
    > java TestArguementProgram --arg1 firstParameter --arg2 secondParameter
    >
    > My question is, how is this format parsed once inside the java program
    > (besides using StringTokenizer)? I have used the Properties class to
    > parse name value pairs from a file, but figured that since its such
    > common convention, there has to be a better approach in java.


    Yeah, it's called GNU Getopt (Google is your friend).

    Dima
    --
    Backwards compatibility is either a pun or an oxymoron. -- PGN
    Dimitri Maziuk, Jun 8, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. I R BABOON
    Replies:
    0
    Views:
    305
    I R BABOON
    Oct 21, 2003
  2. TR
    Replies:
    3
    Views:
    1,201
  3. =?Utf-8?B?RGlmZmlkZW50?=
    Replies:
    3
    Views:
    814
    Juan T. Llibre
    May 13, 2005
  4. Replies:
    1
    Views:
    1,621
    Bjorn Abelli
    Apr 24, 2006
  5. jmborr
    Replies:
    1
    Views:
    396
    Stargaming
    Nov 3, 2007
Loading...

Share This Page