3

I have a war with a simple Jersey REST service , I need to bundle that jar with/with in Jetty in a single fat jar so that it can be run as a whole after being shared. Can some one please point me to instructions on how to do that ? I am unable to find something that is relevant to my usecase.

Thanks in advance!

user1965449
  • 2,849
  • 6
  • 34
  • 51

1 Answers1

3

I believe what you are looking for is to embed Jetty in your jar, so you can start your application with:

java -jar myApp.jar

And your REST service will be up and running, without the need of deploying the war file in Jetty (or tomcat or so...) or care about other dependencies. Is that right?

If so, look for how to embed jetty into your app. Here's some examples:

Embedding Jetty as a Servlet Container

Embedding Jetty

If you could provide more details, that will be great.

EDIT

public static void main(String[] args) throws Exception {
    Server server = new Server(9001);
    Context context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new ServletContainer(new ResourceConfig(YourRestService.class)));
    server.start();
}

Export to .jar, and run in terminal with:

java -jar myApp.jar

Your rest service will be available in port 9001.

Community
  • 1
  • 1
PerGon
  • 113
  • 7
  • Pergon, that is exactly what I want to do , but my application is a WAR and not a jar because it has a Jersey REST service included in it.Will I be able to use the same approach for a WAR using the information you provided in your links . It would be great if I end up with a jar instead of a war. – user1965449 Nov 13 '13 at 15:07
  • Keep in mind that there is not much of a difference between a jar and a war file (basically those files a just the compile class zipped... in the case of the war, there are .jsp, .xml... files). Instead exporting to .war, export to .jar. Make sure you start jetty in the main method (check the links). – PerGon Nov 13 '13 at 19:12