2015-10-19 95 views
0

我們試圖通過從java執行以下命令來創建Windows事件日誌。 但無法在描述(/ d)中添加換行符(\ n)。從java執行時在eventcreate描述消息中添加換行

eventcreate /噸錯誤/ ID爲100/l的對myApp/SO 「MYSOURCE」/ d 「是這樣的日誌\的第一n行,這是第二行」

從Java代碼以上執行:調用Runtime.getRuntime( ).exec(命令);

1.嘗試添加crl + l(^ L)而不是\ n,但它只能從命令行中使用,而不能從java代碼中使用。

回答

0

最後得到了解決:

相反將String作爲參數傳遞給.exec()方法,我們可以傳遞字符串數組。通過這種方式,日誌按摩就可以識別g \ n作爲新行。下面的代碼段現在起作用。

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); 
0

換行字符取決於您的操作系統(\n for Unix, \r\n for Windows and \r for old Macs)上,你應該使用:

String eol = System.getProperty("line.separator"); 
String cmd = "eventcreate /t error /id 100 /l myApp /SO 'mysource' /d 'this is line of the log"+eol+" this is second line'"; 
Runtime.getRuntime().exec(cmd) 

或使用\r\n這是特定的Windows

+0

我曾測試\ r \ n,這是不是working.Also試圖System.getProperty(「line.separator」),但同樣result.I覺得在寫日誌是無法解釋它。 –