Launching and Debugging Tomcat from Eclipse without complex plugins

Modern IDEs like Eclipse provide various Plugins to ease web developement. However, I believe that starting Tomcat as “normal” Java application still provides the best debugging experience. Most of the time, this is because these tools launch Tomcat or any other servlet container as external process and then attach a remote debugger on it. While you’re still able to set breakpoints and inspect variables, other features like hot code replacement don’t work that well.

Therefore I prefer to start my Tomcat just like any other Java application from within Eclipse. Here’s how it works:

This article addresses experienced Eclipse users. You should already know how to create projects, change their built path and how to run classes. If you need any help, feel free to leave a comment or contact me.

We’ll add the Tomcat as additional Eclipse project, so that paths and all remain platform independent. (I even keep this project in our SVN so that everybody works with the same setup).

Step 1 – Create new Java project named “Tomcat7

Step 2 – Remove the “src” source folder

Step 3Download Tomcat (Core Version) and unzip into our newly created project. This should now look something like this:

Step 4 – If you havn’t, create a new Test project which contains your sources (servlets, jsp pages, jsf pages…). Make sure you add the required libraries to the built path of the project

Step 5.1 – Create a run configuration. Select our Test project as base and set org.apache.catalina.startup.Bootstrap as main class.

Step 5.2 – Optionally specify larger heap settings as VM arguments. Important: Select the “Tomcat” project as working directory (Click on the “Workspace” button below the entry field.

Step 5.3 – Add bootstrap.jar and tomcat-juli.jar from the Tomcat7/bin directory as bootstrap classpath.Add everything in Tomcat7/lib as user entries. Make sure the Test project and all other classpath entries (i.e. maven dependencies) are below those.

Now you can “Apply” and start Tomcat by hitting “Debug”. After a few seconds (check the console output) you can go to http://localhost:8080/examples/ and check out the examples provided by Tomcat.

Step 6 – Add Demo-Servlet – Go to our Test project, add a new package called “demo” and a new servlet called “TestServlet”. Be creative with some test output – like I was…

Step 7 – Change web.xml – Go to the web.xml of the examples context and add our servlet (as shown in the image). Below all servlets you also have to add a servlet-mapping (not shown in the image below). This looks like that:

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/demo/test</url-pattern>
</servlet-mapping>

Hit save and restart tomcat. You should now see your debug output by surfing to http://localhost:8080/examples/demo/test – You now can set breakpoints, change the output (thanks to hot code replacement) and do all the other fun stuff you do with other debugging sessions.

Hint: Keeping your JSP/JSF files as well as your web.xml and other resources already in another project? Just create a little ANT script which copies them into the webapps folder of the tomcat – and you get re-deployment with a single mouse click. Even better (this is what we do): You can modify/override the ResourceResolver of JSF. Therefore you can simply use the classloader to resolve your .xhtml files. This way, you can keep your Java sources and your JSF sources close to each other. I will cover that in another post – The fun stuff starts when running multi tenant systems with custom JSF files per tenant. The JSF implementation of Sun/Oracle has some nice gotchas built-in for that case ;-)

Reference: Launching and Debugging Tomcat from Eclipse without complex plugins from our JCG partner Andreas Haufler at the Andy’s Software Engineering Corner blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


one × 3 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close