views:

82

answers:

2

Hi, has anybody found a way how to specify the Java line.separator property on VM startup? I was thinking of something like this:

java -Dline.separator="\n"

But this doesn't interprete the "\n" as linefeed character. Any ideas?

Regards,

Martin.

+5  A: 

I wouldn't do that if I were you. The line-separator is platform specific, and should remain so. If you want to write windows-only or linux-only files, define a UNIX_LINE_SEPARATOR constant somewhere and use it instead.

Bozho
exactly. some things shouldn't be changed. any api that relies on line.separator having a specific value is broken
seanizer
I would appriciate if you consider that other people are not just stupid but have their reasons. Mine is, that a Java program is called from within a shell script. The shell script used stdout of the Java app and processes it further. If you run that script from Cygwin, Java uses Windows linefeeds. However, that causes problems in the script. Hence, I'd like to change Javas default line.separator in case it's run from Cygwin.
Martin
first, you could've shared these details in the question. Second, what is so special about that shell-script that can't be programmed in Java? It doesn't sound resonable to me to have a linux-only java software, that you like to run with Cygwin. Either provide a `.bat` file (like Tomcat, for example), or move the functionality to a Java class. And finally, I don't consider anyone stupid. But people are often inexperienced.
Bozho
Fair enough. I'm just supporting a co-developer here which deals with some legacy software. Hence, there are not much decisions on my part. I'm just the guy for Java questions. And you are my guys for complicated Java questions. ;-)
Martin
+3  A: 

Try using java -Dline.separator=$'\n'. That should do the trick, at least in bash.

Here is a test-run:

aioobe@r60:~/tmp$ cat Test.java 
public class Test {
    public static void main(String[] args) {
        System.out.println("\"" + System.getProperty("line.separator") + "\"");
    }
}
aioobe@r60:~/tmp$ javac Test.java && java -Dline.separator=$'\n' Test
"
"
aioobe@r60:~/tmp$ 
aioobe
Yea ... but you shouldn't.
Stephen C
I can definitely see how it could be useful in, for instance, a test-scenario.
aioobe
Thanks, that solved it from command line. It still doesn't work from within the run config of Eclipse IDE, but that's not important.
Martin