How to configure Tomcat 5 and Eclipse to run Java 5

Author: Kasper B. Graversen, 12/08/08
Keywords: Tomcat, Eclipse, JSP, Java
Abstract: How to get Tomcat 5.0 and Tomcat 5.5 setup in Eclipse to run How to run javac 1.5 (or beyond) compiler for JSP compilation with generics enabled (and other Java 1.5 only features like autoboxing).
subscribe to my RSS feed


Bookmark and Share


How to configure Tomcat 5 and Eclipse to run Java 5

The other day I set up a project using Tomcat 5.0 and Eclipse (the EclipseEE edition). While the server ran the dated Tomcat 5, it luckily supported Java 5. Unfortunately, while Tomcat 5.x is capable of running Java 5, it defaults to older versions. Tomcat 5.0 defaults to using Java 1.3, Tomcat 5.5 defaults to Java 1.4. The next hurdle is to configure the server in EclipseEE. This is quite easy.. once you figure out how. Since I spent a few hours pulling all the strings together, I figured I might as well document my findings here so others hopefully saves a bit of time...

Configure Tomcat 5.x

Older Tomcat's out of the box will complain with the following error message upon asked to serve a JSP containing Java 5 code.

HTTP Status 500 - type Exception report

message
description The server encountered an internal error () that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 6 in the jsp file: /WEB-INF/tags/...tag
Generated servlet error:
C:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\...\..._tag.java:76: 
generics are not supported in -source 1.3
(try -source 1.5 to enable generics)
List<String> results = ...
          ^
1 error

    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
    org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:437)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:497)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:476)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:464)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
    org.apache.jasper.servlet.JspServletWrapper.loadTagFile(JspServletWrapper.java:201)
    org.apache.jasper.compiler.TagFileProcessor.loadTagFile(TagFileProcessor.java:514)
	...
The first step to resolving this issue, is to completely ignore the hint "-source 1.5 to enable generics". That message will take you nowhere! The reason being that it originates from the javac compiler, while you need to configure the jasper JSP compiler. This is fortunately quite easy, although finding out on the Tomcat site is quite convoluted. All you need to do is to edit your TOMCAT_INSTALL\conf\web.xml. Add to the org.apache.jasper.servlet.JspServlet the following lines
    <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> 
such that the servlet looks something like the following
[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>
            <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>
        <load-on-startup>3</load-on-startup>
    </servlet>
 
You have now configured your Tomcat JSP server...

Configuring Eclipse

The EE edition of Eclipse has a nice feature of enabling you to create a Dynamic web project - essentially an exploded war file and manage the server instance for you and set up debugging. Convenient once you know how it works. Basically what the standard setup does, is that it copies the Tomcat configuration files from your install directory onto some obscure path similar to c:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\ The first thing to realize is that it copies the files from your installed Tomcat directory quite frequently, so editing your web.xml in this directory as instructed above will only work for a short time.

You may also try editing the web.xml under the Servers project Eclipse installs when you setup your first server. I had a very hard time making Eclipse pick up changes in this file..

Instead, simply delete your server instance and re-create it. Your changes will now have been picked up and you are ready to go!



Comments

If you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.


If you like this site consider subscribing to my RSS feed or how about subscribing by Email.


Help spread the word

Share this post on your favorite social bookmarking sites:
If you enjoyed this article, found it thought provoking, educative or otherwise good, please link to this page from your page or social bookmarking page. If you have any texts you think are worth publishing on First Class Thoughts, don't hesitate to send me a mail! Quality always welcome.


Bookmark and Share


The most recent contributions
28/07/09 Magic in mathematics II Fun with the number cyclic numbers, and specifically with 142857 as it is the smallest of such numbers.
13/07/09 My top 8 time-saving Firefox shortcuts This article presents my favorite top 8 time-saving shortcuts in Firefox 3.0 and Firefox 3.5. Get to know these and you'll be saving a lot of time. They have been ordered by "the element of most surprise"
20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba".
16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words.
Nothing of interest? Try browsing the entire article archive...