How to run javac 1.5 (or beyond) compiler for JSP compilation in Tomcat 5.5 with generics enabled (and other Java 1.5 only features like autoboxing)

By Angsuman Chakraborty, Gaea News Network
Thursday, February 3, 2005

Target Audience
Java/JSP developers on Tomcat and Application Server administrators.

Problem
Tomcat 5.5 (unlike Tomcat 5.0 and versions below) comes with Eclipse JDT compiler enabled by default for JSP compilation. JDT compiler is not jdk 1.5 compliant as of now.

Solution
To just use the javac 1.5 compiler with Java 1.4 source code compliant JSP pages (you cannot use generics or autoboxing in scripted jsp code and other Java 1.5 features) and Java 1.4 compliant target classes generated, you have to add tools.jar from your %JAVA_HOME%\lib directory to %TOMCAT_HOME%\common\lib.
Then replace the jasper-compiler-jdt.jar with ant.jar in %TOMCAT_HOME%\common\lib. Make sure you have downloaded the latest version of Ant.

To enable 1.5 features in your JSP files (like generics and autoboxing for example) you need to additionally modify %TOMCAT_HOME%\conf\web.xml file.

You need to add two init parameters as shown below in bold:

    < servlet>
        < servlet-name>jsp< /servlet>
        < servlet-class>org.apache.jasper.servlet.JspServlet< /servlet>
        < init-param>
            < param-name>fork< /param-name>
            < param-value>false< /param-value>
        < /init-param>
        < init-param>
            < param-name>compilerSourceVM< /param-name>
            < param-value>1.5< /param-value>
        < /init-param>
        < init-param>
            < param-name>compilerTargetVM< /param-name>
            < param-value>1.5< /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>

The compilerSourceVM and compilerTargetVM parameters indicates the compiler to assume the source is Java 1.5 compliant and the target classes generated will be Java 1.5 compliant.

Side Effect
This also enables Ant. Javac is invoked from Ant.

Please do not ask me further questions on the topic. Please read the Tomcat source code for further explanation, if required.

The procedure has been well tested and works without exception. The instructions are tailored for Windows platforms. Please adapt the instructions (minor changes) suitably for Unix/Linux platforms.

Comments are welcome.

Discussion

Joshua Davies
May 17, 2010: 11:29 am

Thank you for this!

May 29, 2009: 6:49 am

sir,
i had a problem in deploying .war file in j2ee 1.4 . i got error like “DEPLOYING APPLICATION IN DOMAIN FAILED ” .Even i checked my web.xml inside WEB-INF i didn’t get any error inside it nobody solved this error many users asked bout it but never get right response on it. So plz will u give me advice or tips to solve this problem.


Gobi
January 23, 2008: 12:18 am

Hi folks.
I tried all the steps mentioned in this page.
Still am getting the error cannot compile the generics features in the jsp page.

the steps i did

1) copied and pasted the tools.jar of jdk1.5 in common/lib folder
2) then replaced the jasper-compiler-jdt.jar file with lates ant.jar(1.7)
3) and finally i made the web.xml corrections for the compilerSourceVM tag

Kindly help me in this regard


Vasanth
September 13, 2006: 3:46 am

Thanks,
You saved my day.


Suryakant
September 12, 2006: 6:57 am

hi,
I hav installed jdk1.5 at d drive.Now i m trying to run programs for RMI but when i set the path
and trying to compile the program i got an error
i.e.javac is not internal or external command.
Plz tell me how to set the path correctly
i did this:
d:\rmi>set path=d:\jdk1.5\bin
d:\rmi>set classpath=%classpath%;.;


Eric So
August 9, 2006: 4:45 pm

Hi all,

I tried to download the latest tomcat 5.5.17 and replace

jasper-compiler-jdt.jar
jasper-compiler.jar
jasper-runtime.jar

with the latest ones. It works for me.

April 28, 2006: 12:23 pm

Thanks for sharing.


Gene
April 21, 2006: 9:14 pm

I needed one more thing to do to get it to work.
as challanger9 states here

rename or delete jasper-compiler-jdt.jar (I just renamed it to jasper-compiler-jdt.jar.defunct) in your %catalina_home%/common/lib.
Then grab the latest ant (ant.jar is all u really need).
Then add the two parameters above, restart tomcat and BAM….works.


David
February 16, 2006: 2:21 am

The latest main comment with advice was written a year ago, but now I manage to compile JSP - including Java generic declarations - under Tomcat 5.5 when I use Tomcat with eclipse. I cannot remeber doing anything special apart from pointing tomcat server to JRE 1.5 in eclipse.

Am trying to deploy to my hosting company who use JDK 1.5, but it comes up with an error on the website.

Are these or any other special actions still needed?

What init definitions should be looked out for, if upgraded from an older version of TOmcat.

Thanks
David

January 9, 2006: 10:33 pm

[...] How to run javac 1.5 (or beyond) compiler for JSP compilation in Tomcat 5.5 with generics enabled (and other Java 1.5 only features like autoboxing) -Simple Thoughts - Java and Web Technology Blog [...]


Hmm
January 4, 2006: 7:51 am

Thanks man
Thank you very much

October 31, 2005: 9:13 am

@Gana

Have you made the changes as specified in the next page?

I am assuming you have jdk 1.5 installed. Correct?


gana
October 31, 2005: 1:57 am

Hi Tried the sample. unfortunately i still get

generics are not supported in -source 1.4
(try -source 1.5 to enable generics)

error. have tried everything with no success. any ideas would be appreciated

thanks

June 28, 2005: 8:04 am

Thanks Yaroukh. Fixed.

June 27, 2005: 4:31 am

There are still unbalanced tags; ALL [param-name] and [param-value] are closed with [/param].

March 25, 2005: 4:37 am

Yaroukh and others. I figured Bachchu’s comments had an unintentional effect of garbling the comments section. I just updated them with proper escape characters so as to make WordPress happy :)

Actually when I upgraded WordPress to 1.5, it ended up garbling the web.xml file of the post. Anyway I updated the post to enable proper display of the file.

It should now be ok.

let me know if you face any further problems.


Yaroukh
March 11, 2005: 10:41 am

I’m sorry for repeating what has already been pointed out. (The tags were interpreted and invisible therefore.)


Yaroukh
March 11, 2005: 10:34 am

The web.xml sample contains errors. The elements and are ending with instead of and respectively.

February 4, 2005: 1:34 pm

Thanks for pointing it out. Actually I copied it from my web.xml file. So it was copied ok.
Somehow while publishing wp seems to have garbled it.


Anjan Bacchu
February 4, 2005: 12:32 pm

Hi There,

Thanks for the tip.

Tried it — found that the following changes to the xmls make it well-defined.

<!– BEGIN new args –>
<init-param>
<param-name>compilerSourceVM</param>
<param-value>1.5</param>
</init>
<init-param>
<param-name>compilerTargetVM</param>
<param-value>1.5</param>
</init>
<!– END new args –>

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :