Monday, April 6, 2015

How to set Tomcat 7 source level to Java 7? Avoid "Resource specification not allowed here for source level below 1.7" error

I have been facing problem where whenever I was accessing jsp page which were using some Java 7 specific features, the compilation of such JSP pages use to fail with following error:

Resource specification not allowed here for source level below 1.7

To fix this I googled for hours and set up the JDK home correctly on my catalina.sh with env.sh to point to jdk7. But this never solved my problem.

Finally today I hit the gold while reading the apache tomcat jasper documentation. The JSP compiler had to be set to use 1.7 as target and source version.

To do this we need to add two extra init-params in the conf/web.xml file in the tomcat home.
 The JSP servlet configuration should have

     <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>
            <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>
        <load-on-startup>3</load-on-startup>
    </servlet>

Restart the server and boom it works.