Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Pass arguments as name value pairs to java program

Reply
Thread Tools

Pass arguments as name value pairs to java program

 
 
dufffman@gmail.com
Guest
Posts: n/a
 
      06-06-2006
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,

 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      06-06-2006
<> 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[i].equals("--arg1")
arg1 = args[i++];
else if (args[i].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


 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      06-07-2006
"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[i].equals("--arg1")
> arg1 = args[i++];

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

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


--
Thomas


 
Reply With Quote
 
opalpa@gmail.com opalinski from opalpaweb
Guest
Posts: n/a
 
      06-07-2006
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/

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      06-07-2006
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


 
Reply With Quote
 
Dimitri Maziuk
Guest
Posts: n/a
 
      06-07-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
How do I get the column name/value pairs from a ResultSet? laredotornado@zipmail.com Java 1 04-24-2006 04:27 PM
Displaying the all the name-value pairs of the session object? =?Utf-8?B?RGlmZmlkZW50?= ASP .Net 3 05-12-2005 11:54 PM
WebRequest : how to submit name-value pairs? TR ASP .Net 3 01-11-2005 01:23 PM
Handling name value pairs in a web service I R BABOON ASP .Net 0 10-21-2003 12:20 AM



Advertisments