I have the following code that is being read from a file:
private static void writeLogs(String filePath, PrintWriter writer)throws ServletException, IOException { String fullFilePath = AppConfig.getAppDir()+"/auditlogs/"+filePath+".log"; System.out.println("loading log files from --> "+fullFilePath); BufferedReader br = new BufferedReader(new FileReader(fullFilePath)); try { StringBuilder sb = new StringBuilder(); String line; List<String> tmp = new ArrayList<String>(); do { line = br.readLine(); tmp.add(line); }while (line != null); for(int i=tmp.size()-1;i>=0;i--) { if(tmp.get(i)!=null){ sb.append(tmp.get(i)); sb.append(System.lineSeparator()); } } String logs = sb.toString(); writer.write(logs); } finally { br.close(); } }
This works as expected when I deploy to a tomcat server on windows. When I try to do the same in linux, the files are not readable. When checking the tomcat log file, I see:
SEVERE: Servlet.service() for servlet ReadLogsServlet threw exception java.lang.NoSuchMethodError: java.lang.System.lineSeparator()Ljava/lang/String; at com.ericsson.ims.web.servlet.ReadLogsServlet.writeLogs(ReadLogsServlet.java:53) at com.ericsson.ims.web.servlet.ReadLogsServlet.doGet(ReadLogsServlet.java:26) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
The error is due to System.lineSeparator() , but when I check javadoc it says:
Returns the system string of a line separator. It always returns the same value - the initial value of the line.separator system property.
On UNIX systems, it returns "\ n"; on Microsoft Windows systems, this returns "\ r \ n".
Can someone explain why this does not work and how to fix it so that it will work on multiple platforms?
java linux windows tomcat
Sionnach733
source share