Running a web application (WAR) with embedded jett

2020-07-07 11:58发布

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.

标签: java Jetty war
1条回答
我命由我不由天
2楼-- · 2020-07-07 12:04

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();
查看更多
登录 后发表回答