title: Embedded Jetty with Maven example fails to restart: Address already in use
body: <p>When trying to repeatedly run the <a href="http://www.eclipse.org/jetty/documentation/current/maven-and-jetty.html#configuring-embedded-jetty-with-maven" rel="nofollow noreferrer">Embedded Jetty with Maven example</a> in NetBeans, I unfortunately get the error:</p>
  
  <p><a href="https://i.stack.imgur.com/HOUk1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HOUk1.png" alt="NetBeans screenshot"></a></p>
  
  <pre><code>cd /Users/afarber/src/JettyMavenHelloWorld; JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home "/Applications/NetBeans/NetBeans 8.1.app/Contents/Resources/NetBeans/java/maven/bin/mvn" "-Dexec.args=-classpath %classpath org.example.HelloWorld" -Dexec.executable=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
  Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
  Scanning for projects...
  
  ------------------------------------------------------------------------
  Building Jetty HelloWorld 0.1-SNAPSHOT
  ------------------------------------------------------------------------
  
  --- exec-maven-plugin:1.2.1:exec (default-cli) @ hello-world ---
  2016-05-24 19:01:26.748:INFO:oejs.Server:main: jetty-9.0.2.v20130417
  2016-05-24 19:01:26.817:WARN:oejuc.AbstractLifeCycle:main: FAILED ServerConnector@7591083d{HTTP/1.1}{0.0.0.0:8080}: java.net.BindException: Address already in use
  java.net.BindException: Address already in use
      at sun.nio.ch.Net.bind0(Native Method)
      at sun.nio.ch.Net.bind(Net.java:437)
      at sun.nio.ch.Net.bind(Net.java:429)
      at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
      at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
      at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:227)
      at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
      at org.eclipse.jetty.server.Server.doStart(Server.java:309)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
      at org.example.HelloWorld.main(HelloWorld.java:29)
  2016-05-24 19:01:26.818:WARN:oejuc.AbstractLifeCycle:main: FAILED org.eclipse.jetty.server.Server@77a567e1: java.net.BindException: Address already in use
  java.net.BindException: Address already in use
      at sun.nio.ch.Net.bind0(Native Method)
      at sun.nio.ch.Net.bind(Net.java:437)
      at sun.nio.ch.Net.bind(Net.java:429)
      at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
      at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
      at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:227)
      at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
      at org.eclipse.jetty.server.Server.doStart(Server.java:309)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
      at org.example.HelloWorld.main(HelloWorld.java:29)
  Exception in thread "main" java.net.BindException: Address already in use
      at sun.nio.ch.Net.bind0(Native Method)
      at sun.nio.ch.Net.bind(Net.java:437)
      at sun.nio.ch.Net.bind(Net.java:429)
      at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
      at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
      at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:227)
      at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
      at org.eclipse.jetty.server.Server.doStart(Server.java:309)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
      at org.example.HelloWorld.main(HelloWorld.java:29)
  </code></pre>
  
  <p>If the root cause for this is the <a href="http://developerweb.net/viewtopic.php?id=2981" rel="nofollow noreferrer">SO_REUSEADDR</a> limitation when binding listening sockets - then probably <code>ServerSocket.setReuseAddress(true)</code> would help.</p>
  
  <p>But how and where to call it in the <a href="https://github.com/afarber/jetty-newbie/blob/master/JettyMavenHelloWorld/src/main/java/org/example/HelloWorld.java" rel="nofollow noreferrer">HelloWorld.java</a>?</p>
  
  <pre><code>package org.example;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.ServletException;
  import java.io.IOException;
  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.server.Request;
  import org.eclipse.jetty.server.handler.AbstractHandler;
  
  public class HelloWorld extends AbstractHandler
  {
      public void handle(String target,
                         Request baseRequest,
                         HttpServletRequest request,
                         HttpServletResponse response)
          throws IOException, ServletException
      {
          response.setContentType("text/html;charset=utf-8");
          response.setStatus(HttpServletResponse.SC_OK);
          baseRequest.setHandled(true);
          response.getWriter().println("&lt;h1&gt;Hello World&lt;/h1&gt;");
      }
  
      public static void main(String[] args) throws Exception
      {
          Server server = new Server(8080);
          server.setHandler(new HelloWorld());
          server.start();
          server.join();
      }
  }
  </code></pre>
  
  <p>I am using the following <a href="https://github.com/afarber/jetty-newbie/blob/master/JettyMavenHelloWorld/pom.xml" rel="nofollow noreferrer">pom.xml</a>:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
  
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;hello-world&lt;/artifactId&gt;
    &lt;version&gt;0.1-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;name&gt;Jetty HelloWorld&lt;/name&gt;
  
    &lt;properties&gt;
        &lt;jettyVersion&gt;9.3.9.v20160517&lt;/jettyVersion&gt;
    &lt;/properties&gt;
  
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
        &lt;artifactId&gt;jetty-server&lt;/artifactId&gt;
        &lt;version&gt;${jettyVersion}&lt;/version&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
  
    &lt;build&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
          &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
          &lt;version&gt;1.1&lt;/version&gt;
          &lt;executions&gt;
            &lt;execution&gt;&lt;goals&gt;&lt;goal&gt;java&lt;/goal&gt;&lt;/goals&gt;&lt;/execution&gt;
          &lt;/executions&gt;
          &lt;configuration&gt;
            &lt;mainClass&gt;org.example.HelloWorld&lt;/mainClass&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Lars Gendner is correct and I do have HelloWorld processes still running at my Macbook:</p>
  
  <pre><code># ps -ef | grep HelloWorld
    501   944     1   0  6:58pm ??         0:04.20 /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java -classpath /Users/afarber/src/JettyMavenHelloWorld/target/classes:/Users/afarber/.m2/repository/org/eclipse/jetty/jetty-server/9.0.2.v20130417/jetty-server-9.0.2.v20130417.jar:/Users/afarber/.m2/repository/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar:/Users/afarber/.m2/repository/org/eclipse/jetty/jetty-http/9.0.2.v20130417/jetty-http-9.0.2.v20130417.jar:/Users/afarber/.m2/repository/org/eclipse/jetty/jetty-util/9.0.2.v20130417/jetty-util-9.0.2.v20130417.jar:/Users/afarber/.m2/repository/org/eclipse/jetty/jetty-io/9.0.2.v20130417/jetty-io-9.0.2.v20130417.jar org.example.HelloWorld
    501   948     1   0  6:58pm ??         0:03.89 /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java -classpath /Users/afarber/src/JettyMavenHelloWorld/target/classes:/Users/afarber/.m2/repository/org/eclipse/jetty/jetty-server/9.0.2.v20130417/jetty-server-9.0.2.v20130417.jar:/Users/afarber/.m2/repository/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar:/Users/afarber/.m2/repository/org/eclipse/jetty/jetty-http/9.0.2.v20130417/jetty-http-9.0.2.v20130417.jar:/Users/afarber/.m2/repository/org/eclipse/jetty/jetty-util/9.0.2.v20130417/jetty-util-9.0.2.v20130417.jar:/Users/afarber/.m2/repository/org/eclipse/jetty/jetty-io/9.0.2.v20130417/jetty-io-9.0.2.v20130417.jar org.example.HelloWorld
  </code></pre>
  
  <p>And the port 8080 is in use:</p>
  
  <pre><code># netstat -an | grep -w 8080
  tcp46      0      0  *.8080                 *.*                    LISTEN     
  </code></pre>
  
  <p>But of course I do not want to change port number again and again (and spawn more processes).</p>
  
  <p>Instead (being NetBeans and Jetty newbie) I wonder how to properly stop embedded Jetty in NetBeans? (besides of <code>awk /HelloWorld/ '{print $2}'| xargs kill</code> workaround)</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>Adding <code>jetty-maven-plugin</code> to the <a href="https://github.com/afarber/jetty-newbie/blob/master/JettyMavenHelloWorld/pom.xml" rel="nofollow noreferrer">pom.xml</a> and running<code>mvn jetty:run</code> does start a web server at port 8080, but it only displays the red text:</p>
  
  <pre><code>Directory: /
  </code></pre>
  
  <p>Here is the full Maven output:</p>
  
  <pre><code># mvn jetty:run
  [INFO] Scanning for projects...
  [INFO]                                                                         
  [INFO] ------------------------------------------------------------------------
  [INFO] Building Jetty HelloWorld 0.1-SNAPSHOT
  [INFO] ------------------------------------------------------------------------
  [INFO] 
  [INFO] &gt;&gt;&gt; jetty-maven-plugin:9.3.9.v20160517:run (default-cli) &gt; test-compile @ hello-world &gt;&gt;&gt;
  [INFO] 
  [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
  [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  [INFO] skip non existing resourceDirectory /Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/src/main/resources
  [INFO] 
  [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---
  [INFO] Changes detected - recompiling the module!
  [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
  [INFO] Compiling 1 source file to /Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/target/classes
  [INFO] 
  [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
  [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  [INFO] skip non existing resourceDirectory /Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/src/test/resources
  [INFO] 
  [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world ---
  [INFO] No sources to compile
  [INFO] 
  [INFO] &lt;&lt;&lt; jetty-maven-plugin:9.3.9.v20160517:run (default-cli) &lt; test-compile @ hello-world &lt;&lt;&lt;
  [INFO] 
  [INFO] --- jetty-maven-plugin:9.3.9.v20160517:run (default-cli) @ hello-world ---
  [INFO] Configuring Jetty for project: Jetty HelloWorld
  [INFO] webAppSourceDirectory not set. Trying src/main/webapp
  [INFO] webAppSourceDirectory /Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/src/main/webapp does not exist. Trying /Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/target/webapp-tmp
  [INFO] Reload Mechanic: automatic
  [INFO] Classes = /Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/target/classes
  [INFO] Logging initialized @3610ms
  [INFO] Context path = /
  [INFO] Tmp directory = /Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/target/tmp
  [INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
  [INFO] Web overrides =  none
  [INFO] web.xml file = null
  [INFO] Webapp directory = /Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/target/webapp-tmp
  [INFO] jetty-9.3.9.v20160517
  [INFO] Started o.e.j.m.p.JettyWebAppContext@58a63629{/,file:///Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/target/webapp-tmp/,AVAILABLE}{file:///Users/afarber/src/jetty-newbie/JettyMavenHelloWorld/target/webapp-tmp/}
  [INFO] Started ServerConnector@75a118e6{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
  [INFO] Started @4608ms
  [INFO] Started Jetty Server
  [INFO] Using Non-Native Java sun.nio.fs.PollingWatchService
  [WARNING] Quiet Time is too low for non-native WatchService [sun.nio.fs.PollingWatchService]: 1000 &lt; 5000 ms (defaulting to 5000 ms)
  </code></pre>