embedding jetty server problems

2020-07-25 10:06发布

问题:

I am having some struggles embedding a jetty server into a java app. I am using spring to configure the actual objects and I can build the webapp no problems - but I get this problem when starting up the server...

0 [main] INFO test.Server  - Starting server in 'c:/workspace/test/war/' on port 9090
34615 [main] INFO test.Server  - Using resource base: src/main/webapp
34615 [main] INFO test.Server  - Using descriptor file: src/main/webapp/WEB-INF/web.xml
2011-09-05 12:29:36.961:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
36344 [main] INFO test.Server  - Starting Server!
2011-09-05 12:29:37.023:INFO::jetty-6.1H.22
2011-09-05 12:29:37.039:WARN::Failed startup of context org.mortbay.jetty.webapp.WebAppContext@8ab708{/TestServer,file:/C:/workspace/test/war/src/main/webapp/}
java.lang.ClassNotFoundException: org.mortbay.jetty.plus.webapp.EnvConfiguration
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at org.mortbay.util.Loader.loadClass(Loader.java:91)
    at org.mortbay.util.Loader.loadClass(Loader.java:71)
    at org.mortbay.jetty.webapp.WebAppContext.loadConfigurations(WebAppContext.java:859)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:431)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:224)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
    at test.Server.main(Server.java:84)
2011-09-05 12:29:37.054:INFO::Started SocketConnector@0.0.0.0:9090

The problem seems to be with some of my path stuff...the dir structure is straight forward (what maven 'recommends') -

workspace/test/war/ -src/main/java/test/Server.java -src/main/webapp/WEB-INF/web.xml

Other than that it falls into the spring stuff I have setup in the web.xml that has been tried and true (via tomcat).

The relevant code in main is this (otherwise it is properties crap):

svrLogger.info ("Starting server in '" + home + "' on port " + port);

String rsrcBase = "src/main/webapp";
String webXml = rsrcBase + "/WEB-INF/web.xml";

svrLogger.info ("Using resource base: " + rsrcBase);
svrLogger.info ("Using descriptor file: " + webXml);

Server server = new Server(port);
WebAppContext webapp = new WebAppContext();

webapp.setContextPath ("/TestServer");
webapp.setDescriptor (home + "/" + webXml);
webapp.setResourceBase (home + "/" + rsrcBase);
webapp.setParentLoaderPriority (true);
server.setHandler(webapp);

svrLogger.info ("Starting Server!");
server.start();
server.join();
svrLogger.info ("Joined and exiting");

Any help is well...helpful. This is the first time I have tried to make my own web server (so other advice is welcome too!)

回答1:

Try to include

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-jndi</artifactId>
    <version>${jetty.version}</version>
</dependency>

It's trying to load this class because in Jetty JNDI is not configured by default.

This is a good resource to learn more about Jetty

https://jersey.java.net/documentation/latest/index.html

Good luck!