Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a web application (WAR) with embedded jetty server

Tags:

java

war

jetty

I have made a very basic web application that has only one html page. I exported it as a war.

Now I created an embedded jetty server.

public class SimplestServer {

public static void main(String[] args) throws Exception{
    Server server = new Server(8000);

    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setWar("C:\\Users\\User\\Desktop\\jetty\\JettyWar.war");
    server.setHandler(webapp);

    server.start();
    server.join();
}

}

I created a jar for this embedded server. But when I try to run my web application through the embedded server,I get the following error.

C:\Users\User>java -jar C:\Users\User\Desktop\jetty\webServer.jar

C:\Users\User>java -jar C:\Users\User\Desktop\jetty\webServer.jar .. 2014-02-18 22:43:15.400:INFO:oejs.Server:jetty-8.y.z-SNAPSHOT 2014-02-18 22:43:15.490:INFO:oejw.WebInfConfiguration:Extract jar:file:/C:/Users/User/Desktop/jetty/JettyWar.war!/ to C:\Users\User\AppData\Loca \jetty-0.0.0.0-8000-JettyWar.war--any-\webapp 2014-02-18 22:43:15.878:WARN:oejuc.AbstractLifeCycle:FAILED jsp: java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory at org.apache.jasper.servlet.JspServlet.(JspServlet.java:68) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at org.eclipse.jetty.servlet.ServletContextHandler$Context.createServlet(ServletContextHandler.java:1075) at org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:957) at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:514) at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:344) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:791) at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265) at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242) at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717) at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95) at org.eclipse.jetty.server.Server.doStart(Server.java:282) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at com.windows.services.SimplestServer.main(SimplestServer.java:41) Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at org.apache.jasper.servlet.JspServlet.(JspServlet.java:68) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at org.eclipse.jetty.servlet.ServletContextHandler$Context.createServlet(ServletContextHandler.java:1075) at org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:957) at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:514) at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:344) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:791) at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265) at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242) at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717) at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95) at org.eclipse.jetty.server.Server.doStart(Server.java:282) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at com.windows.services.SimplestServer.main(SimplestServer.java:41) 2014-02-18 22:43:15.909:WARN:oejuc.AbstractLifeCycle:FAILED o.e.j.w.WebAppContext{/,file:/C:/Users/User/AppData/Local/Temp/jetty-0.0.0.0-8000-Je .war--any-/webapp/},C:\Users\User\Desktop\jetty\JettyWar.war: java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory at org.apache.jasper.servlet.JspServlet.(JspServlet.java:68) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at org.eclipse.jetty.servlet.ServletContextHandler$Context.createServlet(ServletContextHandler.java:1075) at org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:957) at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:514) at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:344) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:791) at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265) at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242) at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717) at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95) at org.eclipse.jetty.server.Server.doStart(Server.java:282) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at com.windows.services.SimplestServer.main(SimplestServer.java:41) Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at org.apache.jasper.servlet.JspServlet.(JspServlet.java:68) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at org.eclipse.jetty.servlet.ServletContextHandler$Context.createServlet(ServletContextHandler.java:1075) at org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:957) at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:514) at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:344) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:791) at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265) at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242) at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717) at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95) at org.eclipse.jetty.server.Server.doStart(Server.java:282) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) at com.windows.services.SimplestServer.main(SimplestServer.java:41)

I have used jetty 8.1.14. I cant understand why I am getting apache's no class def error when I selected j2ee preview in my target runtime. By the way, I got the same error when I had used apache tomcat in runtime.

Also note that I am not using maven or ant. I dont want to use either. I wish to do it through simple java only.

Thank you in advance for helping out.

like image 966
Parul S Avatar asked Feb 18 '14 17:02

Parul S


People also ask

How do I run a WAR file on a jetty server?

The easiest way to deploy a web application to Jetty server is probably by copying the WAR file into the $JETTY_HOME/webapps directory. Jetty will scan its $JETTY_HOME/webapps directory at startup for web applications to deploy. Our new app will be deployed at /jetty-app context.

Is Jetty an application server or web server?

Jetty provides a web server and servlet container, additionally providing support for HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS and many other integrations. These components are open source and are freely available for commercial use and distribution.

What is embedded Jetty?

last modified July 15, 2022. Jetty can be run in embedded mode. It means that it is not necessary to build a WAR file and deploy it in a standalone Jetty server. Jetty is a software component that can be instantiated and used just like any other POJO (Plain Old Java Object).


1 Answers

A simple solution is to use the jetty-runner. The jetty-runner jar is directly embedded in the jetty distribution (<groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId>)

and then in your main class you just have to do something like that :

jettyRunner = new JettyRunner("/yourContext", "path/to/your/webApp", port);
    jettyRunner.start();
like image 145
Jérôme Gloaguen Avatar answered Oct 26 '22 17:10

Jérôme Gloaguen