10

I've tried most of the suggestions mentioned on stackoverflow but haven't come across a solution yet. The error I'm being presented with is the following.

An error occurred at line: 379 in the jsp file: /application-new-project_process.jsp
Lambda expressions are allowed only at source level 1.8 or above 

I'm using IntelliJ IDEA 2016.2 and have applied these settings.

Project StructureProject, Project SDK to 1.8 (java version "1.8.0_102")

Project StructureProject, Project Language Level to 8.0 - Lambdas, type annotations etc.

SettingsBuild, Execution, DeploymentCompilerJava Compiler, Project bytecode version to 1.8

SettingsBuild, Execution, DeploymentCompilerJava Compiler, Target bytecode version to 1.8

I'm using Tomcat v8.0.36 and have the following for JSP servlets.

<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>compiler</param-name> 
        <param-value>modern</param-value> 
    </init-param> 
    <init-param> 
        <param-name>compilerSourceVM</param-name> 
        <param-value>1.8</param-value> 
    </init-param> 
    <init-param> 
        <param-name>compilerTargetVM</param-name> 
        <param-value>1.8</param-value> 
    </init-param> 
    <init-param> 
        <param-name>suppressSmap</param-name> 
        <param-value>true</param-value> 
    </init-param> 
    <load-on-startup>3</load-on-startup>
</servlet>

Any suggestions will be greatly appreciated!

2 Answers2

19

I use IntelliJ IDEA 2016.3.2, tomcat apache-tomcat-8.5.8, following changes are sufficient for me:
1. Change following file: apache-tomcat-8.5.8\conf\web.xml
2. Modify configuration for

 <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
  1. Add following init params:

    <init-param>
      <param-name>compilerSourceVM</param-name>
      <param-value>1.8</param-value>
    </init-param>
    <init-param>
      <param-name>compilerTargetVM</param-name>
      <param-value>1.8</param-value>
    </init-param>

Finish.

Alexey Alexeenka
  • 891
  • 12
  • 15
  • This is actually correct, my issue was that I modified the configuration for the wrong servlet. –  Jan 12 '17 at 00:39
  • Excellent, thank you! I was using Eclipse Oxygen and had the same problem. Your solution solved it. – Chris Claxton May 25 '18 at 19:59
  • Using Spring STS 3.9.4, made change in Package Explorer under `Servers/Tomcat v8.5 Server at localhost-config/web.xml` and solved for me as well. – Andrew Wynham Nov 13 '18 at 19:18
6

An updated answer for those using Spring Boot and Tomcat. Since there is no XML configuration file for Tomcat in Spring Boot/MVC, I adapted code linked from these spring docs to create a customizer bean in my base Application class. Fixes problems caused by using Java 8 syntax in JSPs in both IntelliJ and Gradle CLI.

If you use Spring 1.x, add a EmbeddedServletContainerCustomizer bean:

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return (ConfigurableEmbeddedServletContainer container) -> {
        TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
        JspServlet servlet = tomcat.getJspServlet();
        Map<String, String> jspServletInitParams = servlet.getInitParameters();
        jspServletInitParams.put("compilerSourceVM", "1.8");
        jspServletInitParams.put("compilerTargetVM", "1.8");
        servlet.setInitParameters(jspServletInitParams);
    };
}

If you use Spring 2.x, add a WebServerFactoryCustomizer bean:

@Bean
public WebServerFactoryCustomizer containerCustomizer() {
    return (WebServerFactoryCustomizer<TomcatServletWebServerFactory>) factory -> {
        Map<String, String> jspServletInitParams = factory.getInitParameters();
        jspServletInitParams.put("compilerSourceVM", "1.8");
        jspServletInitParams.put("compilerTargetVM", "1.8");
        factory.getJsp().setInitParameters(jspServletInitParams);
    };
}
Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
dugsmith
  • 119
  • 3
  • 9