Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue while handling String[] args in Main method

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
Android Python C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic