Wicket and embedded jetty - classNotFoundException

2019-08-27 19:07发布

I'm trying to run my (seam and) wicket app on an embedded jetty server.

I get the following exception:

Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:366)
    at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:337)
    ... 28 more

However, the LoggerFactory class is on my classpath. I tested this as follows:

public class StartJetty {
    public static void main(String[] args) throws Exception {
        ILoggerFactory fac = LoggerFactory.getILoggerFactory(); //this works!

        Server server = new Server();
        ...

Complete class:

public class StartJetty {
    public static void main(String[] args) throws Exception {
        Logger log = LoggerFactory.getLogger(StartJetty.class);

        Server server = new Server();
        SocketConnector connector = new SocketConnector();
        connector.setMaxIdleTime(1000 * 60 * 60);
        connector.setSoLingerTime(-1);
        connector.setPort(8090);
        server.setConnectors(new Connector[] { connector });
        WebAppContext bb = new WebAppContext();
        bb.setParentLoaderPriority(true);
        bb.setServer(server);
        bb.setContextPath("/wicket");
        bb.setWar("C:/wicket/exploded-archives/wicket.war");
        server.setHandler(bb);
        try {
            server.start();
            while (System.in.available() == 0) {
                Thread.sleep(1000);
            }
            server.stop();
            server.join();
        } catch (Throwable e) {
            e.printStackTrace();

            System.exit(100);
        }
    }
}

2条回答
Root(大扎)
2楼-- · 2019-08-27 19:44

1 - update to jetty 7 or jetty 8, your using jetty 6 by the looks of it and that is getting quite gray behind the ears...we are working on jetty-9 at this point (jetty7 if you want servlet 2.5 and jetty8 if you want servlet 3.0 support)

2 - a webapp executes in an isolated classloader so what you are seeing is that working correctly, you need to set the parent class loader priority on the webapp context that you are creating, or use the server/system mechanism to expose just those org.slf4j classes.

See http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading for more information on classloading in jetty.

查看更多
Summer. ? 凉城
3楼-- · 2019-08-27 19:44

I found the answer from Martjin Dashorst here is a good advice for debugging: https://stackoverflow.com/a/13413342/1915920

  1. remove all *.jars from WEB-INF/lib to some tmp folder
  2. restart via service jetty restart or similar
  3. look at the logs for problems or which classes are found to be missing
  4. add some jars back (e.g. grouped by type to speed it up ... orm/db-related, ui-related, different functionality-scopes and their dependencies) to the WEB-INF/lib folder
  5. repeat 2. to 4. till you likely spot the real conflicting problem (or none if the conflicting jars are not needed in your WEB-INF/lib folder at all (anymore)

applying some class loader related stuff to the web.xml did not help me there: https://jira.codehaus.org/browse/JETTY-574

查看更多
登录 后发表回答