Setup Eclipse and Tomcat to play nice together

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
- Open Eclipse
- Create a New Web Project
- Open the Java Perspective
- Right click and choose New -> Project
- Under Web Folder Choose Dynamic Web Project Click Next, Click Next
- Under Project Facets choose Dynamic Web Module, Java, Click Next
- Change Context Root to / Click Finish
Change the Build Path
- Right click the Project and choose Properties
- Choose Java Build Path in the Left Panel, Click the Source Tab
- Change the Default output folder to: yourprojectname/WebContent/WEB-INF/classes
- Click OK
Create Tomcat Server
- Right click and choose New -> Other
- Under Server Folder Choose Server Click Next
- Under Apache Choose Tomcat v5.5 Server Click Next
- Under Tomcat installation directory: Browse to the place where you unzipped the Tomcat installation
- 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
- Choose the project you created from Available projects to Configured projects Click Finish
- Open the Server View, if it’s not displayed already - Window –> Show View–> Other–> Choose Servers under Server
- Save the Perspective so that the Servers View will also be displayed
- The server you just created will appear in the Servers View
- Right Click the server and choose Move to Workspace
- Double click the Server and the Server Overview will be opened.
- Change the Server Name to something meaningful like YourProjectName Server, Save the configuration
- Close the editor and it will ask you if you want to rename the diretory to match the server name, click yes
- Open the server.xml
- 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”>
- Change the port attribute of the Connector node to 80 and the redirectPort attribute to 443
- 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.
Sep 7th, 2007 at 1:08 pm
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.
Sep 7th, 2007 at 1:21 pm
Give this solution a try:
org.eclipse.wst.common.project.facet.core.nature
org.eclipse.wst.common.modulecore.ModuleCoreNature
1. Open .project file for the project to be converted to web project.
2. Insert the following to natures element.
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.
Feb 19th, 2008 at 3:56 am
I’m using Tomcat6 and the eclipse IDE cannot add it, any ideas?
Feb 19th, 2008 at 8:07 am
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.
Mar 19th, 2008 at 5:09 pm
what context in the project settings? What module tab? Don’t see them.
Mar 19th, 2008 at 6:35 pm
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.
Mar 25th, 2008 at 10:30 am
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.
Mar 25th, 2008 at 12:10 pm
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.
Mar 25th, 2008 at 12:32 pm
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.
Mar 25th, 2008 at 12:50 pm
Anytime, sorry I couldn’t be a little more help. Good luck with finding a solution.
May 2nd, 2008 at 4:14 am
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
May 2nd, 2008 at 8:07 pm
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..
Jul 15th, 2008 at 6:17 pm
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!
Jul 30th, 2008 at 7:48 am
WebGuy is great indeed
Aug 20th, 2008 at 11:21 am
=== 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)
Aug 20th, 2008 at 12:32 pm
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.
Sep 4th, 2008 at 11:16 pm
webguy is excellent simply supeerb.
Nov 6th, 2008 at 11:14 am
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.
Nov 6th, 2008 at 11:17 am
@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.
Nov 13th, 2008 at 1:53 pm
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!
Nov 13th, 2008 at 11:06 pm
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.
Nov 19th, 2008 at 10:14 am
You saved the day! Thanks much.
This also helps understanding the internals of how tomcat server is launched from within eclipse.