title: How to embed Jetty and Jersey into my Java application
body: <p>So I'm trying to embed jetty into my web application so that if I package it as a jar someone can just run the jar file without having to worry about configuring a server. However, I'm having some problems setting up my main class so that jetty can access my resource classes. I've looked at tutorials but they haven't given me exactly what I'm looking for. This is what I have so far. </p>
  
  <pre class="lang-java prettyprint-override"><code>package pojo;
  
  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.servlet.DefaultServlet;
  import org.eclipse.jetty.servlet.ServletContextHandler;
  import org.eclipse.jetty.servlet.ServletHolder;
  
  public class Main {
  
      public static void main(String[] args) throws Exception {
          Server server = new Server(8080);
          ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
          context.setContextPath("/");
          ServletHolder h = new ServletHolder(new DefaultServlet());
          h.setInitParameter("javax.ws.rs.Application","resources.DBCollection");
          context.addServlet(h, "/*");
          server.setHandler(context);
          server.start();
          server.join();
      }
  }
  </code></pre>
  
  <p>And I'm trying to map it to this class:</p>
  
  <pre class="lang-java prettyprint-override"><code>package resources;
  
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Request;
  import javax.ws.rs.core.UriInfo;
  
  import pojo.Party;
  
  @Path("/parties")
  public class DBCollection {
  
      @Context
      UriInfo url;
  
      @Context
      Request request;
  
      String name;
  
      @GET
      @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
      public List&lt;Party&gt; getAllParties() throws Exception
      {
          List&lt;Party&gt; list = new ArrayList&lt;Party&gt;();
          list.addAll(DBConnection.getPartyCollection().values());
          return list;
      }
  
      @GET
      @Path("count")
      @Produces(MediaType.TEXT_PLAIN)
      public String getPartyCount() throws Exception
      {
          return String.valueOf(DBConnection.getPartyCollection().size());
      }
  
      @Path("{party}")
      public DBResource getParty(@PathParam("party")String party)
      {
          return new DBResource(url,request,party);
      }
  }
  </code></pre>
  
  <p>Here's my POM file:</p>
  
  <pre class="lang-xml prettyprint-override"><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/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;PartyAPI&lt;/groupId&gt;
      &lt;artifactId&gt;PartyAPIMaven&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;5.1.6&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.codahale.metrics&lt;/groupId&gt;
              &lt;artifactId&gt;metrics-core&lt;/artifactId&gt;
              &lt;version&gt;3.0.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
              &lt;version&gt;1.17.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
              &lt;artifactId&gt;jetty-server&lt;/artifactId&gt;
              &lt;version&gt;9.0.0.RC0&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
              &lt;artifactId&gt;jetty-servlet&lt;/artifactId&gt;
              &lt;version&gt;9.0.0.RC0&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
              &lt;artifactId&gt;jetty-servlets&lt;/artifactId&gt;
              &lt;version&gt;9.0.0.RC0&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;eclipselink&lt;/artifactId&gt;
              &lt;version&gt;2.6.0-SNAPSHOT&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;oss.sonatype.org&lt;/id&gt;
              &lt;name&gt;OSS Sonatype Staging&lt;/name&gt;
              &lt;url&gt;https://oss.sonatype.org/content/groups/staging&lt;/url&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.6&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;createDependencyReducedPom&gt;true&lt;/createDependencyReducedPom&gt;
                      &lt;filters&gt;
                          &lt;filter&gt;
                              &lt;artifact&gt;*:*&lt;/artifact&gt;
                              &lt;excludes&gt;
                                  &lt;exclude&gt;META-INF/*.SF&lt;/exclude&gt;
                                  &lt;exclude&gt;META-INF/*.DSA&lt;/exclude&gt;
                                  &lt;exclude&gt;META-INF/*.RSA&lt;/exclude&gt;
                              &lt;/excludes&gt;
                          &lt;/filter&gt;
                      &lt;/filters&gt;
                  &lt;/configuration&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;package&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;shade&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;transformers&gt;
                                  &lt;transformer
                                      implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /&gt;
                                  &lt;transformer
                                      implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;
                                      &lt;manifestEntries&gt;
                                          &lt;Main-Class&gt;com.resteasy.Star.Main&lt;/Main-Class&gt;
  
                                      &lt;/manifestEntries&gt;
  
                                  &lt;/transformer&gt;
                              &lt;/transformers&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>