Search code examples
javawindowsnewlineevent-logstringescapeutils

Adding newline in the eventcreate description message while executing from java


We are trying to create windows eventlog by executing following command from java. But unable add newline(\n) in the description(/d).

eventcreate /t error /id 100 /l myApp /SO "mysource" /d "this is 1st line of the log \n this is second line"

Executing above from java code : Runtime.getRuntime().exec(command);

1.Tried adding crl+l (^L) instead of \n but it works only from command line not from java code.


Solution

  • Finally got the solution:

    Instead of passing String as parameter to .exec() method we can pass Array of Strings.In this way log massage is identifying \n as new line. Below code snippet now works.

    String[] command = {"eventcreate", "/t", "error", "/id","100","/l", "CustomApp", "/SO","CustomSource","/d","this is 1st line of the log \n this is second line"};       
    Runtime.getRuntime().exec(command);