Forums Register Login

Issue while handling String[] args in Main method

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi Rob,

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

Thanks,
Venkat
+Pie Number of slices to send: Send
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.
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1431 times.
Similar Threads
JavaCaps Test 2, Question # 33
regarding overloaded methods of var args
Process waits forever when using Stringbuffer with more than 2000 strings
PrintStream problems
How to capture the PID when instantiating for a Java program
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:09:34.