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?
1 reply to this topic
#1
Posted 17 May 2011 - 06:07 AM
|
|
|
#2
Posted 17 May 2011 - 08:43 AM
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:
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.
Example:
> runprogram.exe These are my argumentsThe 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


Sign In
Create Account


Back to top









