After a bit of investigation ... you do set the value by passing an option like -Dline.separator=X
on the command line, but it is important how and where you pass it. Firstly, you must pass the property before the class (as a parameter to the java
executable) otherwise it is a parameter passed to the running program and thus not actually setting the 'environment' property.
Take this simple class as an example:
public class TestClass {
public static void main(String[] args) {
System.out.printf("property test.value [%s] property line.separator [%s]",
System.getProperty("test.value"), System.getProperty("line.separator") );
}
}
On Windows Powershell, you need to execute like so ...
PS C:\> java -cp target\classes -D"line.separator=4" TestClass -D"test.value=3"
property test.value [null] property line.separator [4]
You see the line.separator
value is set but the test.class
value is not. On Windows you needed to double-quote the property names that are arguments to the java
executable.
Passing both as parameters to the java
program ...
PS C:\> java -cp target\classes -D"test.value=3" -D"line.separator=4" TestClass
property test.value [3] property line.separator [4]
... to see the correct values received by the program.
To pass a newline, passing -D"line.separator=\n"
does not work as the \n
is treated as a string by Powershell so you must pass the Powershell notation for a newline - [`n]
- to actually get the newline to the java program. eg:
PS C:\> java -cp target\classes -D"test.value=3" -D"line.separator=`n" TestClass
property test.value [3] property line.separator [
]
Above you see an actual newline used in the output.
On unix the syntax is a bit different. You don't need to double-quote the property names but you still need to pass the newline character in a way that causes the shell to actually pass a newline.
Firstly, if we just pass a \n
we see it is passed verbatim:
$ java -cp target/classes/ -Dtest.value=3 -Dline.separator='\n' TestClass
property test.value [3] property line.separator [\n]
But if we tell the shell to process that string it will convert it into a newline:
$ java -cp target/classes/ -Dtest.value=3 -Dline.separator=$'\n' TestClass
property test.value [3] property line.separator [
]
I'm not an expert in the ins and outs of shell scripting, but I can see there appears to be a few ways to pass a newline character depending on your OS and shell. To your specific ask about Windows and cmd
, there is yet more fun. This question gives me the example script I need to run my sample and get a newline output.
I created a bat
script called runjava.bat
to run the java, and define the newline as a variable which is then passed as the parameter.
setlocal EnableDelayedExpansion
(set \n=^
%=Do not remove this line=%
)
echo Line1!\n!Line2
java -cp target\classes -Dtest.value=3 -Dline.separator=!\n! TestClass
Here I run the script and you can see the output has a newline in it.
c:\>.\runjava.bat
c:\>setlocal EnableDelayedExpansion
c:\>(set \n=
)
c:\>echo Line1!\n!Line2
Line1
Line2
c:\>java -cp target\classes -Dtest.value=3 -Dline.separator=!\n! TestClass
property test.value [3] property line.separator [
]
To summarise ... you can override the line.separator
property but you will have to get the syntax just right!