aspose file tools*
The moose likes Java in General and the fly likes Issue while handling String[] args in Main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Issue while handling String[] args in Main method" Watch "Issue while handling String[] args in Main method" New topic
Author

Issue while handling String[] args in Main method

Venkat veramasu
Greenhorn

Joined: Aug 08, 2009
Posts: 4
Hi Gurus,

I know its a basic question. but i dont how it is handled in java.

I have a program that handles 5 String[] args. The input args are going to be

ipaddress time username encryptedpassword role.

10.0.0.0 1306149242287 Admin Tf]Mbp9}`n,$H'Ld0\"Vh|% AdminUser

note that the encrypted password contains " (Double Quotes). Becuase of this the Java main method takes `Tf]Mbp9}`n,$H'Ld0\"Vh|% AdminUser` as a single argument. So if i try to handle args[4] then i get ArrayOutofBoundException

Here is the sample code



When i run this program
C:>java com.xyz.dm.TestArgs 10.0.0.0 1306149242287 Admin Tf]Mbp9}`n,$H'Ld0\"Vh|% Admin
Lenth5
Arument is ==10.0.0.0
Arument is ==1306149242287
Arument is ==Admin
Arument is ==Tf]Mbp9}`n,$H'Ld0"Vh|%
Arument is ==Admin



But if i run the same class through ProcessBuilder and passing these exact argument, im getting the java.lang.ArrayIndexOutOfBoundsException: 4

Here is the code:




Here is the output

In ======== Lenth4
Argument is ==10.78.0.0
Argument is ==1306149242287
Argument is ==Admin
Argument is ==Tf]Mbp9}`n,$H'Ld0Vh|% Admin

Is it a valid behavior ? To have a workaround im passing the encrypted password as the final argument. But if i have the encrypted password in the middle i will have this exception.

Could someone clarify this behaviour

Thanks,
Venkat
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

The problem is certain characters in your encrypted password, Tf]Mbp9}`n,$H'Ld0\"Vh|%.

In Java source code, \" inside a String is not the two characters \ and " but only one, ". The \ is used to escape it. To use it in Java you'd need to use \\\" - the first \ escapes the second one, then the third one escapes the ". Because of this the password contains a ", and that in turn is a special character for command line interpreters.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Venkat veramasu
Greenhorn

Joined: Aug 08, 2009
Posts: 4
Hi Rob,

Thanks for your reply. But the encrypted password comes from calling another function which sends the encryptedpassword.

Thanks,
Venkat
David Freitag
Greenhorn

Joined: May 28, 2011
Posts: 13

It doesn't really matter where the string comes from, what Rob said still stands true. You may want to look into built in Java Encryption to handle your passwords.
You may want to look into This article.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue while handling String[] args in Main method