Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change tomcat compiler

Tags:

java-7

tomcat

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.

like image 955
Lenny Markus Avatar asked Jul 21 '11 18:07

Lenny Markus


People also ask

How do I change my compiler version?

Select the command Project ‣ Project Settings and then the Compile options tab. Select the desired Fix Version and click on OK. ⇒ The change is immediately effective.

How do I change my compiler to STS?

To change the compiler version in eclipse you need to go to Windows > Preferences > Java > Compiler. There you will see Compiler Compilation Level where in drop-down you will see all compiler version available in system.

Is Tomcat a compiler?

Tomcat compiles it internally to some class file that you're not aware of (its stored internally in Tomcat), Tomcat loads this file in runtime and treats it as a compiled Java class.

How do I change the default compiler in eclipse?

Step 1: Right click on selected java project and then click on Properties. Step 2: Click on Java Compiler and then perform the changes. Whenever we change the settings, eclipse rebuilds the project.


1 Answers

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.

like image 110
mindas Avatar answered Oct 08 '22 17:10

mindas