GreatWebGuy Self-proclaimed greatness is a hard thing to prove.

17Mar/0744

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

  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.

Comments (44) Trackbacks (0)
  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.

  23. hi all,

    i have developed a dyanamic web application using eclipse ganymede and tomcat server.The applcation is working fine.But for starting the server and the application i have to start the eclipse and the tomcat server correspondingly.again for stoping the application we have to stop the the server and close the eclipse.

    Is there any way to directly start and stop the server and the applcation without starting the eclipse.

  24. Hi All,
    Thats a good aricle given for hot deployment using eclipse ad tomcat. I am stuck with a problem. i have a dynamic we project deployed in eclipse(ganymede)and works fine with tomcat deployed from within the IDE but the only problem is that after making any changes to my .java file or to my .jsp files it does not Hot deploy it..I need to run my ant script and then it works fine ..I have made my project setup as mentioned above and it does not help me in anyway for hot deployment..Any suggestions are greatly appreciated..

  25. Hi Webguy…
    was wondering if you were able to hot-deploy with the config u mentioned…(I couldn’t – do have a similar config)

    Thanks

  26. Thx…I’ve got it to work too.

  27. Hi WebGuy
    I have created a travel guide system which uses a sax parser to read an xml file. Unfortunatly when we try to load the application with the XML file it doesent initiate because of the xml file
    could u help in the situation?
    Thanks

  28. Hi Webguy,
    Can you help me out?
    I am using Tomcat 5.5 and Ganymede. I import a war file and link it to Ganymede’s Tomcat. All ok. Everything starts.

    However, I want to add a few classes and hot deploy.

    any time I use ganymede to build the class, it restarts the server so it’s not a true hot deploy – any ideas?

  29. Hi Webguy,

    I am using eclipse 3.3 and maven to build my project. The problem i face is whenever i change my java class, clean the project and start the server to debug my code the change doesn’t gets reflected. The change appears in java only when i run the maven build again. Any solution for this.

  30. Hey Breako/sriram,

    If you want to see the changes in your class files without restarting tomcat set in TOMCAT_HOME/conf/context.xml by default it is false.

  31. Hi

    Iam using Eclipse 3 and I integrated the WAS6, Its working fine. But only problem is from Eclipse Iam able to stop and start the WAS6 application server, But I can’t hot deploy. So everytime I change the code I need to create the ear file and deploy it through WAS6 admin console.

    Could you please let me know is there any way to hot deploy the Application in Eclipse -was6 server combination.

    Thanks in advance,
    Babu

  32. The method mentioned is applicable for all eclipses.
    I’ll repeat again.

    If u r not using maven, ant or any build tool then u can create a web project no need to do whatever being told here.

    If u r using spring, hibernate, struts, maven or any other framework. In this case Suppose your project name is “PROJECT_NAME”.

    Now right click on left pane choose other->server

    Selecting server and clicking next will open a new window, u dono need to change anything in this window except if you would like to change server name.

    Now right click on the server folder which has been just created and select properties->project reference = PROJECT_NAME

    Now u would see server tab on bottom part of the main window where u see consol.
    U can see that a new server is being created. Double click on it and then do whatever told in the original post.

  33. Hiiiii guys,
    hey i just wanted to know what settings you need to do to run two different web applications on the same instance of tomcat server and also how to run two different web applications on two different instance of tomcat server.

  34. My eclipse does not see Tomcat at all, I am running Eclipse Indigo and Tomcat 7.0.21, can anyone help me please. How can I get Eclipse to see my Tomcat installation?

    Thanks in advance

  35. I couldnt find dynamic web project in the new tab of my Eclipse v3.2… pls Give me a solution to create it or browse it…

    • Hi,

      Am getting following error when i try to run my prog.
      Can any one help.

      The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: .:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java

  36. WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:manoj’ did not find a matching property.

  37. Hi greatwebguy

    Thanks for your article. I have the same error as manoj. Am running Eclipse for Java EE developers with tomcat (along eclipse 3.7) on ubuntu 11.10. Could you help? I could send you the long error log, if that’s useful.
    many thanks

  38. I need to check Sessions Administration from eclipse for Tomcat 5.5 ? Is there a plugin? how do i check sessions of applications in tomcat which is in eclipse

  39. My tomcat is on a remote server ?


Leave a comment

(required)

No trackbacks yet.