Setup Eclipse and Tomcat to play nice together

Eclipse and Tomcat
Eclipse is by far my favorite IDE, which makes Rational Application Developer bearable. The one thing I’ve been spoiled on in RAD is being able to run the WebSphere Test Environment inside the development environment and be able to have some level of hot deploy when modifying classes or jsp’s in the web project. This brings me to my problem with Eclipse and Tomcat, Eclipse provides great support for adding a Tomcat server to run in your IDE, but the default classpath set up forces you to do a build and restart almost everytime you modify a JSP or Class, this is a real pain. The other pain is that by default when you set up a web project, eclipse decides to build your src files into a build directory rather than just building them directly into the web project’s WEB-INF/classes directory.

This is what I’m here to solve. I’m assuming your using Eclipse 3.2 (Callisto) with the Web Tools installed, JDK 1.5 or 1.6, and Tomcat 5.5 or 6. May work in earlier versions, but I haven’t set up any projects like that recently.

Create a new Dynamic Web Project

  1. Open Eclipse
  2. Create a New Web Project
  3. Open the Java Perspective
  4. Right click and choose New -> Project
  5. Under Web Folder Choose Dynamic Web Project Click Next, Click Next
  6. Under Project Facets choose Dynamic Web Module, Java, Click Next
  7. Change Context Root to / Click Finish

Change the Build Path

  1. Right click the Project and choose Properties
  2. Choose Java Build Path in the Left Panel, Click the Source Tab
  3. Change the Default output folder to: yourprojectname/WebContent/WEB-INF/classes
  4. Click OK

Create Tomcat Server

  1. Right click and choose New -> Other
  2. Under Server Folder Choose Server Click Next
  3. Under Apache Choose Tomcat v5.5 Server Click Next
  4. Under Tomcat installation directory: Browse to the place where you unzipped the Tomcat installation
  5. Under JRE: Make sure jre1.5.x is selected, you may need to add the jre if a different one is installed on your computer. Click Next
  6. Choose the project you created from Available projects to Configured projects Click Finish
  7. Open the Server View, if it’s not displayed already - Window –> Show View–> Other–> Choose Servers under Server
  8. Save the Perspective so that the Servers View will also be displayed
  9. The server you just created will appear in the Servers View
  10. Right Click the server and choose Move to Workspace
  11. Double click the Server and the Server Overview will be opened.
  12. Change the Server Name to something meaningful like YourProjectName Server, Save the configuration
  13. Close the editor and it will ask you if you want to rename the diretory to match the server name, click yes
  14. Open the server.xml
  15. Change the appBase attribute of the Host node to contain the absolute path to your workspace directory ex. <Host appBase=”d:\web” autoDeploy=”true” name=”localhost” unpackWARs=”false” xmlNamespaceAware=”false” xmlValidation=”false”>
  16. Change the port attribute of the Connector node to 80 and the redirectPort attribute to 443
  17. Change the docBase attribute of the Context Node to contain the absolute path to your WebContent folder ex. <Context docBase=”d:\web\yourprojectname\WebContent” path=”/” reloadable=”true” source=”org.eclipse.jst.j2ee.server:forensicsalvage”/>

You should be able to start the server up from the Server View and view you application and make live changes to your jsp’s, which should only require a refresh now.

22 Responses to “Setup Eclipse and Tomcat to play nice together”

  1. Hi,

    What if you have pre existing web applications? I have a huge webapplication that I have been working on since a while. I upgraded to Eclipse Europa and then turned on the “Servers” view. In there i right click and say “New -> Server”. Select Tomcat 5.5 and point the right Apache Tomcat 5.5 installation on my machine. Next it shows me the screen where I can “Add and Remove Projects”. I do not see any available projects. I am guessing this is because I did not create my project as a “New Web Project”. Can I change the project type to be a “Web Project” instead of it being a “Java Project” and will that show up in that list of “Available Projects” or is there another way.

  2. Give this solution a try:
    1. Open .project file for the project to be converted to web project.
    2. Insert the following to natures element.
    org.eclipse.wst.common.project.facet.core.nature
    org.eclipse.wst.common.modulecore.ModuleCoreNature
    3. Open the properties for the project and go to the page for Project Facets
    4. Add Java and Dynamic Web Module facets to the project.
    5. Done.

    Let me know how it works out.

  3. I’m using Tomcat6 and the eclipse IDE cannot add it, any ideas?

  4. It’s definitely different with Tomcat 6 and Eclipse Europa, I was actually messing with the server last night. These directions definitely don’t apply to that environment. They seem to have fixed a couple of things and it doesn’t require all of this work around, you should be able to just set your context to / in the project settings and create a server and set up the Modules in the module tab of the server configuration to also run the application at a path of /. The only change I made to the server.xml was to run on port 80 instead of 8080.

  5. what context in the project settings? What module tab? Don’t see them.

  6. If you right click the web project and choose properties, then click Web Project Settings, you should see the Context Root, set this value to / and click Ok. Now, if you’ve created a Tomcat Server, you’ll be able to double click the server and open the server Overview, at the bottom of the Overview there’s a tab for Modules, make sure your Web Module also has a Path of /. Let me know if you have any other questions.

  7. Is it possible to use a variable for the docbase? I got the absolute and relative pathing working fine. I have similar projects in multiple workspaces depensing on the branch / version I’m working on and depending on other project dependencies.

  8. I couldn’t tell you for sure, I don’t think there’s a way to specify a variable for the docbase, since it’s in the server.xml. If just using Tomcat you could set up a separate server for the branch if there’s not too many configuration changes.

  9. thank you for taking the time to respond. There are indeed ALOT of configurations/possibilities. I’m thinking I may toy around with one of the scripting languages to dynamically generate the context on startup. Perhaps bootstrapping a configuration in. I wonder if using an entity replacement would work… hmm. time to play.

  10. Anytime, sorry I couldn’t be a little more help. Good luck with finding a solution.

  11. Hi I am trying to convert my java project(runs smoothly with tomcat) to dynamic web project. I saw ur comment on project facates but I dont find the Dynamic Web facate
    listed for my project in RAD

  12. naresh,

    Thanks for the comment, I’ve added a new post, Converting a Java project into a Dynamic Web Project in Eclipse, hopefully to help you and other people out with the same problem. Hope it helps..

  13. Just wanted to thanks a lot for this detailed overview! I actually hadn’t realized that Eclipse creates a copy of the main server.xml and wondered for quite a while why my modifications to the file had no effect. Of course modifying the server.xml in the new server project did the trick. So merci beaucoup for the information! :-)

  14. WebGuy is great indeed ;)

  15. === How to create a Dynamic Web Project ===

    1. Start Eclipse.exe

    2. Create a new project: New > Web > Dynamic Web project

    3. Give the project a name

    4. Specify the location of Tomcat servlet engine by setting the Target Runtime:
    Target Runtime > New > Apache Tomcat 5.5 > Next > Browse > (Directory: …..\tomcat) > Finish

    5. (Accept Sun Java EE licence)
    6. (Accept Java EE perspective)

  16. Thanks Simple :)

    This article is a bit outdated and mostly unnecessary. My “current” recommendation, upgrade to Ganymede, and use Tomcat 6. If you upload directly via SCP or FTP then it’s a great idea to point your web project build path to compile classes into /WebContent/WEB-INF/classes, makes the uploading easier. Server configs work much better in Ganymede and don’t really require you to touch the server.xml at all, you can configure context and ports from the config page for the server.

  17. webguy is excellent simply supeerb.

  18. This is great, thanks for this information. Your last comment about using Ganymede (which I am now using) leaves me wondering if I’m missing something. Is there some documentation around specific to using Tomcat and Eclipse Ganymede that you know of? I’ll go looking but if you happen to have a link I’d love to go read that too. Thanks again.

  19. @BillK No documentation per se, I’ve just noticed that support for Tomcat in Ganymede is much better, requires less screwing around with the server.xml if any at all.

  20. i basically follow the entire installation and with only a minor change from your instructions (set docbase of the Context node to ‘/’) i get the tomcat instance to start up.

    however, i cannot get any jsp, html or other file to display on the page. when i enter any location in the url bar, i get:
    SEVERE: Error, processing connection
    and an IndexOutOfBoundsException from the BufferedInputStream.read method

    still groping the web for some info but anything you can do to help would be appreciated. almost there, i can feel it!

  21. Jsut to follow up - i use a manual setup right now: add the bootstrap.jar file to the project library and then point to the Bootstrap main class and manually add the program arg ’start’ and vm arg ‘-Dcatalina.home=”…”‘ for the run configuration.

    it works fine, but your solution seems a bit more flexible and standard.

    thanks for any help you can provide.

  22. You saved the day! Thanks much.

    This also helps understanding the internals of how tomcat server is launched from within eclipse.

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>