Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

command line arguments and quote marks

843798Jan 19 2007 — edited Jan 22 2007
I need to send a block of text as a single command line argument to a java program.

In particular, I want the
	String[] args
param of my java class's main to contain just a single element.

I am running JDK 1.5.0_10 on windoze xp, and I fully realize that the behavior I describe below is almost certainly due to bizarreness in the msdos shell, and has nothing to do with java, but I am submitting it here in the hopes that someone knowledgeable has seen this before and can help.

To provide a concrete illustration of the behavior I see, consider this toy class that merely echos its command line args:
public class CommandLineEcho {
	public static void main(String[] args) {
		for (int i = 0; i < args.length; i++) {
			System.out.println("args[" + i + "] = " + args);
}
}
}


This simple excecution
	java CommandLineEcho a b c

	args[0] = a
	args[1] = b
	args[2] = c
behaves as expected.

Now suppose you want to get a single command line argument out of text like the above which has spaces in it.

In msdos, you should be able to put double quote marks around it.

Simple cases, such as this excecution
	java CommandLineEcho "a b c"
	
	args[0] = a b c
again behave as expected.

Unfortunately, consider somthing more realistic and complex, like this excecution:
	java CommandLineEcho "cd ..\  &&  dir .\"
	
	args[0] = cd ..\  &&  dir ."
This is unexpected: note that the text was interpreted as a single string, and the leading quote mark was dropped, which is all correct, but the final quote mark was retained, which is bizarre.

With my version of msdos, I find that I can get the correct behavior simply by dropping the final quote mark i.e. use
	java CommandLineEcho "cd ..\  &&  dir .\
Alternatively, if I use parentheses inside the expression to group the subcommands, I can still use the logically consistent pair of quotes and everything works. For instance, excecuting
	java CommandLineEcho "(cd ..\)  &&  (dir .\)"
	
	args[0] = (cd ..\)  &&  (dir .\)
works.

Anybody know why msdos does the strange command line behavior that it does? Have a decent website that explains all this?

I did some web searching and could not find anything.

Typing cmd /? in my dos shell indicates that special chars can sometimes lead to funny behavior with quote marks. Unfortuately, this documentation is strictly for invocation of cmd, and not how cmd actually behaves once it is running.

Experimenting, I found that in the examples above, the offending char is actually the \ char. For instance, excecuting
	java CommandLineEcho "cd ..  &&  dir ."
	
	args[0] = cd ..  &&  dir .
behaves as expected.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 19 2007
Added on Jan 19 2007
4 comments
889 views