NoSuchMethodError when deployed on Linux

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?

+8
java linux windows tomcat
source share
4 answers

It seems that you are not using the same version of Java between Linux and Windows.

As you can see in javadoc , System.lineSeparator() does not exist in java <7.

So, if you want to get the line separator on java <7, you should use:

 System.getProperty("line.separator") 
+11
source share

It seems that you are using different versions of the JDK for Windows and Linux.

  • On Windows, the JDK is 7, which has the System.lineSeparator ( ) method .
  • The same method does not exist in previous versions of the JDK (check the System class in Java 6), which works in your Linux window.

For backward compatible code, use System.getProperty("line.separator") .

+2
source share

As others have pointed out, the problem is with the versions. I would dare to say that just changing the JVM is not enough. Your program may check the expected version at startup to avoid such problems in the future :

 String runtimeName = System.getProperty("java.runtime.name"); String runtimeVersion = System.getProperty("java.runtime.version"); ... check if JVM is the expected one, log some informative message if not ... 

You can also do this for other settings (Locale ...), but they depend on the nature of your project. The idea is that a dead program is better than a cripple (unsuccessfully fast), another idea is that you have to fix the problem once and for all (find errors once), you can find additional tips here: http: // pragprog .com / the-pragmatic-programmer / extracts / tips

+1
source share

System.lineSeparator() is introduced in Java 7 . It looks like you are using an earlier version on your Linux machine. You can use System.getProperty("line.separator"); to get it or upgrade the java version on your Linux machine.

0
source share

All Articles