Jump to content

problem with command line arguments

- - - - -

  • Please log in to reply
1 reply to this topic

#1
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
class CommandLine {
public static void main(String args[]) {
for(int i=0; i<args.length; i++)
System.out.println("args[" + i + "]: " +
args[i]);
}
}
while executing above code if i give the input "what is happening", without the quotes then the program works fine but as soon as i give the input "what's happening" the program get's stuck with greater than sign on the terminal. but again when i give"what\'s happening" the program works fine. why is this so and what to do to get rid of this problem?

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You can't "get rid" of the problem. On the command line, single and double quotes are special characters. They are used to represent strings that may contain spaces, so that the command line parser doesn't split your string up into multiple arguments.

Example:

> runprogram.exe These are my arguments

The above fictional program is executed, and the operating system passes into it 4 arguments: ('These', 'are', 'my', 'arguments'). There are no spaces contained in these strings. However, if you were to invoke the program this way:

> runprogram.exe 'These are my arguments'

Then the operating system would only feed one argument to the program: ('These are my arguments'). The spaces are part of the string, and there is one argument total.

So to answer your question, this isn't an issue with Java at all. It's how the command line parser works with special characters. All single and double quotes must be escaped with the backslash character to be included in the arguments as a literal character.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users