如何更改tomcat编译器

发布于 2024-11-25 15:02:22 字数 411 浏览 1 评论 0原文

我正在尝试使用新的 Java 7 字符串开关功能。

但Tomcat不合作。

我已经确保 tomcat 在 java 7 下运行,但似乎它没有在 java 7 下编译。

我已将以下内容添加到 web.xml 文件中的 jsp servlet 条目下,

    <init-param>
        <param-name>compiler</param-name>
        <param-value>C:/Program Files/Java/jdk1.7.0/bin/javac.exe</param-value>
    </init-param>

但它似乎不起作用。

任何提示将不胜感激。

I'm trying to use the new Java 7 switch on strings feature.

But Tomcat is not cooperating.

I've made sure that tomcat is running under java 7 but it seems that it's not compiling under it.

I've added the following to the web.xml file, under the jsp servlet entry

    <init-param>
        <param-name>compiler</param-name>
        <param-value>C:/Program Files/Java/jdk1.7.0/bin/javac.exe</param-value>
    </init-param>

but it doesn't seem to do the trick.

Any tips would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拥有 2024-12-02 15:02:22

我们正在运行 Tomcat 6 并且遇到了同样的问题。我们的解决方案是:

  • tomcat/lib/ecj-3.3.1.jar 替换为 ecj-3.7.2.jar(可以取自最新的 Tomcat 7 版本) ;
  • 将其添加到tomcat/conf/web.xml

    <前><代码>...

    jsp
    org.apache.jasper.servlet.JspServlet
    <初始化参数>
    <参数名称>fork
    <参数值> false

    <初始化参数>
    <参数名称>xpoweredBy
    <参数值> false

    <初始化参数>
    <参数名称>compilerSourceVM
    <参数值>1.7

    <初始化参数>
    <参数名称>compilerTargetVM
    <参数值>1.7

    <启动时加载>3

当然,更简单的替代方案是安装 Tomcat 7,但这可能不适合每个人。

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.

南…巷孤猫 2024-12-02 15:02:22

我们使用jdk8和tomcat6遇到了同样的问题。将值为 1.8 的 compilerSourceVMcompilerTargetVM 添加到 conf/web.xml 中,当代码带有字符串或 lambda 表达式的 switch 时,仍然无法编译 jsp。但是,将tomcat 6.0.53默认lib中的ecj-4.3.1.jar替换为ecj-4.6.1.jar(可以在maven存储库中找到),jsp将能够成功编译。

$ 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)

希望这对那些坚持为 tomcat6 升级到 jdk8 的人有所帮助。

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.

阿楠 2024-12-02 15:02:22

你在这里混合一些东西。您希望 JSP 使用 Java 7 进行编译,而不是让 tomcat 使用 Java 7 运行。Tomcat 使用 Eclipse Java 编译器,但尚不支持 Java 7。

编辑:我做了一些挖掘。如您所见 这里,使用的是Eclipse内置的编译器。 Tomcat 7.0.19 附带的编译器是 ECJ 3.7,它将支持 Java 7,而不是 3.7.1 之前的版本。

Your mixing something here. You want your JSPs compile with Java 7 and not have the tomcat run with Java 7. Tomcat uses the Eclipse Java Compiler which is does not have Java 7 support yet.

Edit: I did some digging. As you can see here, the built-in Eclipse compiler is used. The compiler distributed with Tomcat 7.0.19 is ECJ 3.7 which will support Java 7 not before 3.7.1.

烟花易冷人易散 2024-12-02 15:02:22

根据所使用的Tomcat版本,tomcat提供的JSP引擎使用特定的默认Java版本来编译和运行JSP。

Tomcat 7 使用 1.6版本

compilerSourceVM - 源文件兼容哪个 JDK 版本
和? (默认值:1.6)

compilerTargetVM - 生成的文件兼容哪个 JDK 版本
和? (默认值:1.6)

Tomcat 8< /strong> 使用1.7版本

compilerSourceVM - 源文件兼容哪个 JDK 版本
和? (默认值:1.7)

compilerTargetVM - 生成的文件兼容哪个 JDK 版本
和? (默认值:1.7)

Tomcat 9< /strong> 使用1.8版本

compilerSourceVM - 源文件兼容哪个 JDK 版本
和? (默认值:1.8)

compilerTargetVM - 生成的文件兼容哪个 JDK 版本
和? (默认值:1.8)

当然,如果默认值不符合您的要求,您可以更改使用的版本。

您可以通过修改
声明了 org.apache.jasper.servlet.JspServlet servlet
在您的全局 $CATALINA_BASE/conf/web.xml 中。

例如,tomcat 8 发行版的 web.xml 是这样定义 JspServlet 的:

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

例如指定 1.8 版本作为 JSP 文件的源和目标,您应该按以下方式更改它:

...
<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>  
     <!-- added 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>
     <!-- end added params -->
    <load-on-startup>3</load-on-startup>
</servlet>
...

According to the Tomcat version used, the JSP engine provided by tomcat uses a specific default version of Java to compile and run JSP.

Tomcat 7 uses the 1.6 version

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

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

Tomcat 8 uses the 1.7 version

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)

Tomcat 9 uses the 1.8 version

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

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

Of course, you may change the versions used if the default values don't match to your requirements.

You can do that by modifying the init parameters of the
org.apache.jasper.servlet.JspServlet servlet declared
in your global $CATALINA_BASE/conf/web.xml.

For example the web.xml of the tomcat 8 distribution defined the JspServlet in this way :

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

For example to specify the 1.8 version both as source and target for JSP files, you should change it in this way :

...
<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>  
     <!-- added 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>
     <!-- end added params -->
    <load-on-startup>3</load-on-startup>
</servlet>
...
月野兔 2024-12-02 15:02:22

您是否尝试过将 compilerSourceVM 设置为 1.7 或 7 ? (恐怕我不能声称知道这是否有效)

Have you tried setting compilerSourceVM to 1.7 or 7 ? (I'm afraid I cann't claim to know if this works or not)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文