How to change tomcat compiler
Asked Answered
E

5

15

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.

Electroballistics answered 21/7, 2011 at 18:10 Comment(0)
G
12

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.

Gain answered 4/8, 2012 at 16:1 Comment(2)
Awesome. In the end we were able to upgrade to Tomcat 7, but it certainly looks like your solution would have done the trick.Electroballistics
Also you can add this to your application's web.inf instead.Rubefaction
B
2

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.

Bainbridge answered 27/2, 2019 at 6:8 Comment(0)
N
0

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.

Navigator answered 23/7, 2011 at 22:46 Comment(2)
Michael-O. I'm trying to get everything to run / compile on Java 7. I already have Tomcat running on 7, I'm just missing the capability to compile. That's why I'm looking for a way to move away form the default compiler.Electroballistics
You should rather contact the tomcat users mailinglist. Mark Thomas is really active on that one.Navigator
M
0

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>
...
Monteith answered 23/11, 2017 at 18:29 Comment(0)
P
-1

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

Phosphorescent answered 21/7, 2011 at 19:42 Comment(1)
Joel, thanks for the reply, but it didn't work :( I set both compilerSourceVM and compilerTargetVM, tried with 1.7 and 7 and got the same resultElectroballistics

© 2022 - 2024 — McMap. All rights reserved.