使用嵌入式jetty服务器运行Web应用程序(WAR)

时间:2014-02-18 17:26:09

标签: java jetty war

我制作了一个只有一个html页面的非常基本的Web应用程序。我把它作为战争出口了。

现在我创建了一个嵌入式码头服务器。

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();
}

}

我为这个嵌入式服务器创建了一个jar。但是当我尝试通过嵌入式服务器运行我的Web应用程序时,我收到以下错误。

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:\用户\用户\应用程序数据\洛卡   \ jetty-0.0.0.0-8000-JettyWar.war- -any- \ webapp 2014-02-18   22:43:15.878:警告:oejuc.AbstractLifeCycle:失败的jsp:   java.lang.NoClassDefFoundError:org / apache / juli / logging / LogFactory   java.lang.NoClassDefFoundError:org / apache / juli / logging / LogFactory           在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   资源)           at java.lang.reflect.Constructor.newInstance(Unknown Source)           在java.lang.Class.newInstance(未知来源)           at org.eclipse.jetty.servlet.ServletContextHandler $ Context.createServlet(ServletContextHandler.java:1075)           在org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:957)           在org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:514)           在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)           在org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)           在org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)           at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)           在org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)           在org.eclipse.jetty.server.Server.doStart(Server.java:282)           at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)           在com.windows.services.SimplestServer.main(SimplestServer.java:41)   引起:java.lang.ClassNotFoundException:   org.apache.juli.logging.LogFactory           在java.net.URLClassLoader $ 1.run(未知来源)           在java.net.URLClassLoader $ 1.run(未知来源)           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)           在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   资源)           at java.lang.reflect.Constructor.newInstance(Unknown Source)           在java.lang.Class.newInstance(未知来源)           at org.eclipse.jetty.servlet.ServletContextHandler $ Context.createServlet(ServletContextHandler.java:1075)           在org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:957)           在org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:514)           在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)           在org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)           在org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)           at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)           在org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)           在org.eclipse.jetty.server.Server.doStart(Server.java:282)           at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)           在com.windows.services.SimplestServer.main(SimplestServer.java:41)   2014-02-18 22:43:15.909:警告:oejuc.AbstractLifeCycle:失败   o.e.j.w.WebAppContext {/,文件:/ C:/Users/User/AppData/Local/Temp/jetty-0.0.0.0-8000-Je   的.war - -any- / web应用/},C:\用户\用户\桌面\码头\ JettyWar.war:   java.lang.NoClassDefFoundError:org / apache / juli / logging / LogFactory   java.lang.NoClassDefFoundError:org / apache / juli / logging / LogFactory           在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   资源)           at java.lang.reflect.Constructor.newInstance(Unknown Source)           在java.lang.Class.newInstance(未知来源)           at org.eclipse.jetty.servlet.ServletContextHandler $ Context.createServlet(ServletContextHandler.java:1075)           在org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:957)           在org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:514)           在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)           在org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)           在org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)           at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)           在org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)           在org.eclipse.jetty.server.Server.doStart(Server.java:282)           at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)           在com.windows.services.SimplestServer.main(SimplestServer.java:41)   引起:java.lang.ClassNotFoundException:   org.apache.juli.logging.LogFactory           在java.net.URLClassLoader $ 1.run(未知来源)           在java.net.URLClassLoader $ 1.run(未知来源)           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)           在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   资源)           at java.lang.reflect.Constructor.newInstance(Unknown Source)           在java.lang.Class.newInstance(未知来源)           at org.eclipse.jetty.servlet.ServletContextHandler $ Context.createServlet(ServletContextHandler.java:1075)           在org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:957)           在org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:514)           在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)           在org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)           在org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)           at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)           在org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)           在org.eclipse.jetty.server.Server.doStart(Server.java:282)           at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)           在com.windows.services.SimplestServer.main(SimplestServer.java:41)

我使用过码头8.1.14。 当我在目标运行时选择j2ee预览时,我无法理解为什么我得到apache没有类def错误。顺便说一句,当我在运行时使用apache tomcat时,我得到了同样的错误。

另请注意,我不使用maven或ant。我也不想使用它们。我希望只通过简单的java来实现。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:7)

一个简单的解决方案是使用jetty-runner。 jetty-runner jar直接嵌入码头分布(<groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId>

然后在你的主要课堂上,你只需做类似的事情:

jettyRunner = new JettyRunner("/yourContext", "path/to/your/webApp", port);
    jettyRunner.start();