Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Pass arguments as name value pairs to java program

Thread view: 
dufffman@gmail.com - 06 Jun 2006 22:39 GMT
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,
Thomas Fritsch - 06 Jun 2006 23:27 GMT
> I have seen java programs executed as below..
>
[quoted text clipped - 4 lines]
> 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.

Signature

Thomas

Thomas Fritsch - 07 Jun 2006 09:20 GMT
> A simple idiom to process such argument arrays is:
>
[quoted text clipped - 4 lines]
>            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
>   }

Signature

Thomas

opalpa@gmail.com opalinski from opalpaweb - 07 Jun 2006 17:50 GMT
> Hi,
>
[quoted text clipped - 8 lines]
>
> 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
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Chris Uppal - 07 Jun 2006 18:58 GMT
opalpa@gmail.com 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
Dimitri Maziuk - 08 Jun 2006 00:41 GMT
dufffman@gmail.com sez:
> Hi,
>
[quoted text clipped - 6 lines]
> 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
Signature

Backwards compatibility is either a pun or an oxymoron.                  -- PGN



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.