简体   繁体   中英

Tomcat uses a Java 8 SDK but won't compile JSP with Java 8 language feature. How to fix it?

I think I'm looking at a Tomcat configuration issue with my current problem. I'm developing a Java Servlet and JSP application for Tomcat 8.5.8, and have begun to use Java 8 language features (specifically method references and streams). On my local machine running jdk1.8.0_211 the new code works, compiling JSP at runtime as it is meant to do.

On our testing server (RH Linux) the new JSP fails to compile (but the rest of the app works). The error is

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 135 in the jsp file: /jsp/directory/page.jsp
Method references are allowed only at source level 1.8 or above

I didn't set up the server myself, but it runs some build of Java 8, and has the same Tomcat version (8.5.8), so I suspect there's a configuration file somewhere that's telling it to compile the JSP at an older language level. (The evidence is that it knows what a Method reference is but refuses to compile it; an exception rather than an error.)

Where might a configuration be hiding that tells Tomcat to compile JSP at an older language level?

Per https://tomcat.apache.org/tomcat-8.0-doc/jasper-howto.html ,

The servlet which implements Jasper is configured using init parameters in your global $CATALINA_BASE/conf/web.xml.

compilerSourceVM - What JDK version are the source files compatible with? (Default value: 1.7) compilerTargetVM - What JDK version are the generated files compatible with? (Default value: 1.7)

To give an example, if your web.xml file contains the following code, it will compile JSP to the 1.8 language level:

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>

    <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>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

I had this problem with JDK1.8 and Tomcat9.0 and navigated all of google.

My solution: Reinstall Tomcat9, very very clear and Start Ok!

Something in Tomcat crashed and we dont know!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM