Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue while handling String[] args in Main method

 
Venkat veramasu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Venkat veramasu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

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

Thanks,
Venkat
 
David Freitag
Greenhorn
Posts: 13
Android C++ Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic