aspose file tools*
The moose likes Beginning Java and the fly likes Passing Parameters to main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Parameters to main" Watch "Passing Parameters to main" New topic
Author

Passing Parameters to main

Guy Rich
Ranch Hand

Joined: May 03, 2011
Posts: 33

Hello all ... I'm passing two parameters to my "main" method.
a path, and a "name" e.g. java APPC_LU62.Runtime.LU62XnsCvr C:\APPC_LU62\CA_DMV ABDSI000
1) Do the passed parameters have to be within quotes ? i.e. "C:\APPC_LU62\CA_DMV ABDSI000"

2) Does the "path" parameter require the "double" backslash ?

3) What's a good technique for determining if the parameters have been passed?
e.g. testing the length of the argument string passed ?

Thanks

Guy
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

It took me about 1 minute to write some code that let me test what you are asking about.

1) if a single parameter has a space in it, it needs to be in quotes. The space is the default (only?) character used to determine what the individual params are.

2) not inherently, no. Although, depending on what you want to DO with it, it might.

3) length is a good start. You may want to do additional verification...i.e. if one should be a number, you'll probably want to do some kind of string-to-integer conversion.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Guy Rich
Ranch Hand

Joined: May 03, 2011
Posts: 33

Thank you again Fred ... So I can copy the arguments into a couple of string variables then check the length of each one ?

e.g. String parm0 = arg[0] ;
String parm1 = arg[1] ;

if (parm0.length < 3)
{ ...send_error_mesg }

if (parm1.length < 2)
{ ...send_error_mesg }

The path parameter will be used to locate files at runtime ...

e.g. StringBuffer full_path new StringBuffer() ;

full_path.append(parm0) ;
full_path.append("\\") ;
full_path.append("file_name.txt") ;

Comments and/or suggestions most welcome

Thanks

Guy
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

copying them into Strings like that seems a waste. param1 doesn't tell me any more or less than args[0]. I could see copying it into a better-named variable like 'path_to_executable' and 'executable_name'.

I guess there are two things you need to check:

1) did they pass in the correct number of params. This is quick and easy, although not TECHNICALLY necessary. But if you know you need to have two, you can immediately deal with it if they only give you 1. Plus, that lets you avoid checking to be sure args[1] is not null.

2) For each parameter you get, you should validate it in whatever way is appropriate for you. Does the path need to be an existing directory? Can you create it if it doesn't exist? Does the file need to be there already? etc.


I am honestly not sure if you need the double-backslashes in the string when you input it on the command line, although my GUESS is no, you don't. My suggestion would be to try it and see what works, which is what I would have to do to get you a definitive answer.
Jared Malcolm
Ranch Hand

Joined: May 02, 2011
Posts: 54

Guy Rich wrote:2) Does the "path" parameter require the "double" backslash ?


My recommendation......use the "/" and don't worry about it. Using the forward slash (like in linux) allows Java to determine what the OS needs (Java will convert to a "\" if the os requires it).


SCJA 6 (Studying for SCJP 6)
Guy Rich
Ranch Hand

Joined: May 03, 2011
Posts: 33


Fred & Jared Thank you both very much.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

Guy Rich wrote:2) Does the "path" parameter require the "double" backslash ?


You would only use the double backslash when the context requires you to escape backslash characters. By far the commonest context which requires that is the Java string literal. For example the Java string literal "a\\b" consists of three characters, namely a and \ and b. There's a couple of other obscure places in the Java world which require that escaping, but elsewhere it isn't necessary.

In this case you're asking about whether the Windows command line requires you to escape backslashes. And no, it doesn't.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Parameters to main