tags:

views:

260

answers:

2

I need to pass a formatted string in a command line so that the input text is presented formatted in a JFrame:

Hello
The sun is shinning!

How do I put a '\n' in a Windows batch file?

java -cp . Test "Hello\nThe sun is shinning!"

I cannot change the program, it is expecting a string as a command line parameter to be shown in a TextBox, and I want to pass multiple lines.

Thanks

+2  A: 

I don't think it's possible.

Bear in mind that the "\n" escape sequence is only recognised by the Java compiler, and doesn't mean anything otherwise.

So what you have here is a Windows batch file language issue, and as far as I know there's no way to put an embedded NL in the parameters in Windows.

What you could do, however, is solve the problem with "\n" mentioned above, and actually have your code recognise the escape sequence:

param = param.replaceAll("\\n", "\n");

The backslash character is quoted twice in the first string, since you actually want to match a literal backslash character.

There's also a function in the Apache Commons library that'll do this:

import org.apache.commons.lang.*;
param = StringEscapeUtils.unescapeJava(param);
Alnitak
I appreciate your comments, and I totally agree with you if I could change the code. The problem is that I cannot change the Java program, I need to pass formatted string in the command line.
any reason you can't write a wrapper (using the above functions) that then invokes the real program with the unescaped parameters?
Alnitak
A: 

It's a bit ugly but you should be able to do it using echo and &&.
Try this:

java -cp . Test (echo Hello&&echo The sun is shining!)

A tab doesn't need escaping - you can put it directly into your batch file.

Greg