简体   繁体   English

Tomcat 使用 Java 8 SDK 但不会编译具有 Java 8 语言功能的 JSP。 如何解决?

[英]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.我想我正在查看当前问题的 Tomcat 配置问题。 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).我正在为 Tomcat 8.5.8 开发 Java Servlet 和 JSP 应用程序,并且已经开始使用 Java 8 语言功能(特别是方法引用和流)。 On my local machine running jdk1.8.0_211 the new code works, compiling JSP at runtime as it is meant to do.在我运行 jdk1.8.0_211 的本地机器上,新代码可以正常工作,在运行时按预期方式编译 JSP。

On our testing server (RH Linux) the new JSP fails to compile (but the rest of the app works).在我们的测试服务器 (RH Linux) 上,新的 JSP 无法编译(但应用程序的其余部分可以运行)。 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.我自己没有设置服务器,但它运行了一些 Java 8 版本,并且具有相同的 Tomcat 版本(8.5.8),所以我怀疑某处有一个配置文件告诉它以旧语言编译 JSP等级。 (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?告诉 Tomcat 在旧语言级别编译 JSP 的配置可能隐藏在哪里?

Per https://tomcat.apache.org/tomcat-8.0-doc/jasper-howto.html ,根据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.实现 Jasper 的 servlet 使用全局 $CATALINA_BASE/conf/web.xml 中的 init 参数进行配置。

compilerSourceVM - What JDK version are the source files compatible with? compilerSourceVM - 源文件与什么 JDK 版本兼容? (Default value: 1.7) compilerTargetVM - What JDK version are the generated files compatible with? (默认值:1.7) compilerTargetVM - 生成的文件与什么 JDK 版本兼容? (Default value: 1.7) (默认值: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:举个例子,如果你的web.xml文件包含以下代码,它会将 JSP 编译到 1.8 语言级别:

<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.我在使用 JDK1.8 和 Tomcat9.0 时遇到了这个问题,并浏览了所有谷歌。

My solution: Reinstall Tomcat9, very very clear and Start Ok!我的解决办法:重装Tomcat9,非常非常清楚,Start Ok!

Something in Tomcat crashed and we dont know!!! Tomcat 中的某些东西崩溃了,我们不知道!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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