How to start and stop Jetty – revisited

I mentioned in my previous post that I would blog about some of the things I learnt while putting together the Seam / JSF versus Wicket “perfbench“.

A while back I posted about how to start and stop Jetty from Ant – useful for those using the Jetty downloaded distribution. In this post I show how to cleanly shutdown a Jetty instance started in “embedded” mode. This tip may be useful for those using Jetty in a continuous integration build – for e.g. when Selenium tests are involved.

Info on starting an embedded Jetty instance is out there but I was not able to find ways to cleanly shutdown – other than hacks like this.

Update 2009-04-07: just found a blog post by Stephen Haberman that has a detailed explanation of WAR-less Development with Jetty

Here’s the code to start Jetty. The trick is to spawn a thread with a socket listening on another port (8079 in this case) that we can connect to later:

package mypackage;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

public class Start {

    private static Server server;

	public static void main(String[] args) throws Exception {
		server = new Server();
		SocketConnector connector = new SocketConnector();
		server.setConnectors(new Connector[] { connector });
		WebAppContext context = new WebAppContext();
        Thread monitor = new MonitorThread();

    private static class MonitorThread extends Thread {

        private ServerSocket socket;

        public MonitorThread() {
            try {
                socket = new ServerSocket(8079, 1, InetAddress.getByName(""));
            } catch(Exception e) {
                throw new RuntimeException(e);

        public void run() {
            System.out.println("*** running jetty 'stop' thread");
            Socket accept;
            try {
                accept = socket.accept();
                BufferedReader reader = new BufferedReader(new InputStreamReader(accept.getInputStream()));
                System.out.println("*** stopping jetty embedded server");
            } catch(Exception e) {
                throw new RuntimeException(e);


The “MonitorThread” in the inner class above stops the embedded Jetty server if a line feed is received. So the code to stop Jetty is pretty simple. Here we duly send “\r\n” to port 8079:

package mypackage;


public class Stop {

    public static void main(String[] args) throws Exception {
        Socket s = new Socket(InetAddress.getByName(""), 8079);
        OutputStream out = s.getOutputStream();
        System.out.println("*** sending jetty stop request");

So how does one use this from Ant? Easy !

<target name="jetty-cycle">               
        <java classname="mypackage.Start" classpathref="test.classpath" fork="true"/>
                <socket server="" port="8080"/>
            <antcall target="my-tests"/>                            
            <java classname="mypackage.Stop" classpathref="test.classpath"/>                 

Full disclosure: I adapted the approach from the Jetty code you can find over here ;)

18 Responses to How to start and stop Jetty – revisited

  1. whitelassiblog says:

    a very nice and informative blog indeed !


  2. vatsan says:

    Super useful, thanks!

  3. Shihab says:

    COuld you please tell me how can we load 3d view of images from set of images. Like I have many image slices. I would like to view those images in a 3D view. Could you please tell me this?

  4. Peter Thomas says:

    @Shihab huh!? why me ?

  5. orange80a says:

    What does refer to?


  6. Pingback: Why you should use the Maven Ant Tasks instead of Maven or Ivy « Incremental Operations

  7. djohn says:

    Can we use the above code for SSL connection also, if not then how?

  8. szyszy says:

    Probably a beginner question, but..
    Why Jetty can only be stopped by another monitor thread?

  9. Thanks!

    That helped me a lot!!!

  10. Taner Diler says:

    helpfully article. I’m deploying a war file to embedded jetty. It works when I call server.start(). But When I explore a service, jetty gives me ClassNotFoundException. I think the embedded server doesn’t load classes (located /WEB-INF/classes) in war. How can I specify the classes location to class loader?

  11. Pingback: Embedding Jetty with Netbeans 7 (and Maven) | Giant Flying Saucer

  12. mariomanumanu says:


    Very useful article.

  13. Noah DeMarco says:

    All you need to do is put a “; server.stop();” in between the start() and the join(). This will stop the server after a line feed is entered in the console.

  14. Peter Thomas says:

    All you need to do is put a “; server.stop();” in between the start() and the join(). This will stop the server after a line feed is entered in the console.

    Of course, and that is how the maven jetty:run plugin is implemented. But as mentioned in the post, this approach is useful for those running a hands-free build for e.g. Selenium tests + continuous integration.

  15. davotoula says:

    I was looking for a clean solution to shut down embedded jetty and this helped.

    I’m a bit surprised that that org.eclipse.jetty.server.Server doesn’t do this for you.

    Out of interest, do you know how the jetty-maven-plugin implements stop port and stop command?



  16. Christian says:

    That’s very helpful, thanks.
    But I believe there must be solution without opening another port on the system. Is it still the way to implement a server shutdown in the actual version?

  17. Pingback: Automatiser ses tests fonctionnels avec Ant | Blog Xebia France

  18. fucking genius, thanks a lot.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 33 other followers

%d bloggers like this: