Jetty and Eclipse Integration

The Jetty web server is a standards compliant J2EE container that is regularly updated to conform with the latest servlet and JSP specifications. Because of its embeddable nature, it is a popular choice with developers who need to incorporate a servlet container /web server in their applications. It is also a natural choice to quickly debug and test web applications that are being developed using the Eclipse IDE.

Jetty Launcher as an Eclipse Plugin

Simplicity notwithstanding, setting up a Web application for debug and testing on Jetty server as a separate command line process can slow down the development cycle considerably. This is especially true for extremely short “change-build-view” cycles. The extra effort can be attributed to

  • context switching between the Jetty console (command shell) and the Eclipse IDE
  • establish visual traceability between console messages and the corresponding source codes.
  • lack of visual debugging capabilities and step through of source code.

The obvious solution is to have a Jetty launcher as an Eclipse plugin for better IDE integration. Unfortunately, there are some practical issues with this scheme, the primary one being that of version mismatches.

As open-source products, both Eclipse and Jetty undergo a change in version quite frequently. For Jetty, the structure of the distribution, in terms of necessary files and folders, keep changing with every major release. Eclipse plugins, on the other hand, are well known for not being cross compatible (backward and forward) with different versions of the IDE.

By far, the most popular plugin for Eclipse and Jetty integration is the Jetty launcher (see Resources and Links at the bottom). Unfortunately, this plugin does not work with latest versions of Jetty (6.x) and Eclipse (3.2.x) due to changes in the structure of Jetty distribution.

A Simple Solution

A good thing about the Jetty distribution is that it uses a single bootstrap jar (start.jar) as the launcher and the entry point to the main servlet container. The location of different libraries and the necessary files and folders for configuration and logging are stored as metadata information in the file org/mortbay/start/start.config within the bootstrap jar. In a pinch, it is possible to extract this file, make modifications as necessary and update the bootstrap jar with the latest contents.

A simple solution to a universal integration would therefore involve setting up the Jetty server as a Java project and execute the start.jar as an external application from within Eclipse. The exact steps to perform this setup are as explained in the following sections.

Step-by-Step Integration

For the purpose of this article, we shall assume the target version of Eclipse to be 3.1.2 and that of Jetty to be 6.1.1. The process enumerated below should remain more-or-less the same for different versions of Jetty and Eclipse.

Step 1: Create Jetty Server Project

Create a Java project in Eclipse called “Jetty Server”. The name is not significant, you can name it anything as long as it makes sense. This project is the one that will be used to launch Jetty as an application from within Eclipse.

Copy the necessary files from the Jetty 6.1.1 distribution including sub-folders to the “Jetty Server” project. The project tree should now look somewhat like this:

Right-click on the project name, select Properties in the popup menu to get the properties dialog box. Add start.jar to the list of libraries in the Java build path.

Step 2: Create Web Application Project

Create your Web application as a separate project under the same Eclipse workspace. The folder structure of a sample Web application is as provided below:

You need to make sure that the Web application project defines a web root folder for setup with Jetty. In the example above it is the folder sampleapp_webroot.

Step 3: Putting it all Together

One nice thing about Jetty 6.x is the support for application context for hot-deployment of applications under development. To make use of this feature, create a file called sampleapp.xml under the folder contexts. The contents of this file should look somewhat like this:

<Configure class="org.mortbay.jetty.webapp.WebAppContext">
  <Set name="contextPath">/app</Set>
  <Set name="war">../sample_app/sampleapp_webroot</Set>
  <Set name="extractWAR">true</Set>
  <Set name="copyWebDir">false</Set>
  <Set name="defaultsDescriptor">
    <SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml
  </Set>
</Configure>

This essentially loads the expanded web application under the path contextPath and maps the same for access using the URL http://<server_name&gt;:<port>/app. Note that relative path names are allowed for the contextPath attribute.

Step 4: Running the Server

Select the “Jetty Server” project within Eclipse. From the main menubar select Run and Run … to bring up the configuration dialog box. Create a new run configuration under Java Application as shown below:

The main class to launch is org.mortbay.start.Main. Save the settings by clicking on the Apply button.

Now press Run to start the Jetty server. If everything goes fine, you will see the Jetty output in the Eclipse console pane. Next time you can start the Jetty server using the Ctrl+F11 shortcut key within Eclipse (Java perspective).

Distribution

For mid-sized development teams, repeating the above steps on multiple desktops can be a time consuming and error-prone process. Having a single distribution of a “Jetty Server” project should help.

Right click on the “Jetty Server” project in Eclipse and select Export from the popup menu. In the export dialog box choose the export destination to be of type “Archive File”. In the next screen, select the output archive file path and click Finish.

Note: Make sure that all items under the “Jetty Server” project are selected for export including the .project and .classpath entries under the root folder.

For installation on a fresh desktop, take the distribution archive and unzip the same in a suitable folder on the target machine. Under Eclipse, create a new Java project from existing source. For the source path, select the folder where you unzipped the distribution archive.

Eclipse Debugging

It is also possible to enable Eclipse debugging for the Web application under development in Jetty. To do so, select the “Jetty Server” project within Eclipse. From the main menubar select Run and Debug … to bring up the configuration dialog box. Creating a new run configuration for debugging is the same as that for a normal startup as explained in Step 4 above. Note that you can press F11 (shortcut key) to launch the previously executed debug session.

In case your Web application contains libraries (jar files) that are under active development as part of the overall application, you would need to step through the source of those libraries as well. To enable such debugging, right click on the library jar in Eclipse, select Properties from the popup menu and attach the corresponding source.

Resources and Links

[1] The Jetty Webserver homepage is at http://jetty.mortbay.org.

[2] The Jetty Launcher plugin for Eclipse can be obtained from http://jettylauncher.sourceforge.net.

reddit

7 Comments

  1. Jan Bartel says:

    Just thought you’d like to know that I’ve recently submitted a patch to the JettyLauncher folks to make it work with Jetty 6. Here’s hoping its accepted and applied soon!

    Also, the Jetty project is looking into how we can improve even further Jetty webapp development in Eclipse, so stay tuned!

    regards
    Jan

  2. indroneel says:

    Jan,

    That’s great news!! Is the patch available for download from the Jetty Launcher site?

    I would still recommend the integration mechanism captured in this article only for cases where the plugin way of integration fails.

    Regards, Neel

  3. Alan says:

    I couldn’t get the Jetty Launcher to work — continually says it is not a valid a Jetty installation!!!

  4. za says:

    Hi, I found your site by googling about Jetty and Eclipse. I found that jetty-launcher didn’t work with Eclipse 3.x and JRE 1.6 (CMIIW). Then I try run-jetty-run which works with Eclipse 3.x and JRE 1.6

    Could you do me some favour, post simple source code web application example (Hello world web application?). I can’t found my web application running on the localhost.

    Thanks!

  5. Deckard Cain says:

    This is the best solution I have found! I tried with 6.1.14 version.

    I was using tomcat at development platform and jetty at production. Now I can use jetty in both places and debug my code! I found that Jetty is faster to start and also to reach the breakpoint in the code. The only difference is clicking debug button instead of tomcat button (old tomcat plugin for eclipse).

    Just a tip about Step 1: there more folders than the image shows. Copy everything if you found any errors when starting the server. Then you can delete ‘examples, extras, licenses’

  6. Aditya says:

    Done all the steps as mentioned above, but unable to run …..
    Jetty is started but displays nothing instead it shows Page not found error when tried to run in an browser …
    Thanks

Leave a Comment