How to change tomcat compiler

We ran to the same problem, using jdk8 and tomcat6. Adding compilerSourceVM and compilerTargetVM with value of 1.8 to conf/web.xml will still not able to compile jsp when the code has switch with strings or lambda expression. However, replace ecj-4.3.1.jar from default lib come with tomcat 6.0.53 with ecj-4.6.1.jar (which can be found in maven repository), jsp will be able to compile successfully.

$ file ./work/Catalina/localhost/_/org/apache/jsp/test1_jsp.class
./work/Catalina/localhost/_/org/apache/jsp/test1_jsp.class: compiled Java class data, version 52.0 (Java 1.8)

Hope this help anyone who stuck with upgrading to jdk8 for tomcat6.


We are running Tomcat 6 and had the same problem. Our solution was to:

  • replace tomcat/lib/ecj-3.3.1.jar with ecj-3.7.2.jar (can be taken from the latest Tomcat 7 release);
  • add this to tomcat/conf/web.xml

    ...
    <servlet>
      <servlet-name>jsp</servlet-name>
      <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
      <init-param>
          <param-name>fork</param-name>
          <param-value>false</param-value>
      </init-param>
      <init-param>
          <param-name>xpoweredBy</param-name>
          <param-value>false</param-value>
      </init-param>
      <init-param>                                    <!-- this should be added -->
          <param-name>compilerSourceVM</param-name>
          <param-value>1.7</param-value>
      </init-param>
      <init-param>
          <param-name>compilerTargetVM</param-name>
          <param-value>1.7</param-value>
      </init-param>                                   <!-- last added line -->
      <load-on-startup>3</load-on-startup>
    </servlet>
    

The simpler alternative is, of course, to install Tomcat 7 but this might not be an option for everyone.

Tags:

Tomcat

Java 7