jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Escape sequences in the args[] array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Escape sequences in the args[] array" Watch "Escape sequences in the args[] array" New topic

Escape sequences in the args[] array

Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
I wonder if anyone can help.
I am trying to pass standard \ escape sequences into my java console program on the command line into the args[] array, but it does not seem to be translating them.

Here is my code:

If I run the program and pass the first parameter as:

I get the output:

Why is this ? Is it because args[0] is an array member and is handled differently ? Surely it's still a string object in the same way that my other examples are ?

Any help with this would be very much appreciated,


Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Escape sequences are handled by the Java compiler, and therefore they must appear in the Java source code. The compiler will see, for example, a '\' followed by a 'n', and replace them both with a single newline character.

If the characters '\' and 'n' are read at runtime from a file or from the command line, they're just two separate ordinary characters.

To reiterate, escape sequences have no meaning at all to Java when a program is running; they only mean something to the compiler when they appear in source code.

Embedding special characters into the arguments to a Java program is actually not a Java issue at all -- it's an operating system issue. If your OS's command line supports a way to type those characters, then you can do it, but if it doesn't, then you're out of luck. On Linux, under most shells, you can simply type

java MyProgram 'line1

and Java will get the three separate lines of input as a single argument with embedded newlines. I have no idea if anything similar is possible on Windows.

[Jess in Action][AskingGoodQuestions]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If you do need to treat \n in a command-line argument as meaning new-line, then you could process each command-line argument to do so.

You would use String.replaceAll("\\n", System.getProperty("line.separator")), I think. The double-backslash here means "really a backslash". It is better to use the system property, rather than "\n", because there are, at least theoretically, platforms where "\n" is not the right new-line sequence to use.
[ November 23, 2004: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
In the first two cases, a String literal is processes by the Java compiler and all occurences of \n are replaced by the unicode character '\u000A' early in the compilation process.

In the last case, the command line arguments are processed by the operating system. The handling of eacape sequences is specific to the OS and command shell you are using. In your case, \n is not treated specially in the command line by your OS. However, you can process these arguments any way you wish with StringTokenizer.

Mike Gershman
SCJP 1.4, SCWCD in process
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Ernest - thanks for your explaination, I now understand why it does not work.

Peter - thanks for your solution, which I like the sound of; however, I'm having trouble getting it to work.
So, the String.replaceAll method replaces all occurances of the first string with the 2nd. No problem.
I can get this to work with any string combo passed as the first string - any that is without a \ !!!
I've tried all sorts to try to fool it, but it just wont find and replace \n (but as mentioned, it will do just n or any other string)
To try to fool it I've tried all sorts as the first parameter:
"\n" (I know the compiler will replace this as a real cr so wont work, but worth a try anyway !)
defining a string with the value "\\n" or "\\"+"n" and passing the reference of the string to the method

Nothing works !

Any ideas ?

And the StringTokenizer suggestion ?? Yes, I can see how that may work (or use String.split), but will it suffer in the same way as replaceAll in that it wont recognise the \ properly ??
And of course, it will make my overall code very messy as I would then need to deal with an array rather than a string


Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
OK, I've found the answer (or at least one answer) and its messy as you like !

As mentioned in my previous posting, I can not pass \ or \\ to the replaceAll method no matter how I try.
So, I got a bit creative ....

Basically, I create an empty StringBuffer and use the append method 3 times to append 2 \'s and the n. I then pass the StringBuffer to the replaceAll method with the toString() method !!!

It works, but I ain't happy with it !
Simon J Birch

Joined: Apr 07, 2004
Posts: 2
Hi Nathan,

I've just been experimenting with this, and I've got a slightly smaller solution. i.e.

I think what is happening is this
  • The compiler evaluates "\\\\n" to "\\n"
  • The underlying Pattern object evaluates "\\n" to "\n"

  • Nathan Russell
    Ranch Hand

    Joined: Aug 18, 2004
    Posts: 75
    Wow, thats a neater solution !
    I tried 1 slash, 2 slashes and out of sheer desperation even 3 at one point !! - but never 4 !! Doh !
    I'll rework my code with your solution as it is much better.

    Thank you to all who provided input to this one;


    I agree. Here's the link: http://aspose.com/file-tools
    subject: Escape sequences in the args[] array
    It's not a secret anymore!