Pass arguments as name value pairs to java program

D

dufffman

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,
 
T

Thomas Fritsch

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.
 
T

Thomas Fritsch

Thomas Fritsch said:
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
}
 
O

opalpa

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
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
C

Chris Uppal

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
 
D

Dimitri Maziuk

(e-mail address removed) 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top