Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassNotFoundException when using Jetty 9.4.14 as Embedded Server #3219

Closed
0Steve0 opened this issue Dec 20, 2018 · 12 comments
Closed

ClassNotFoundException when using Jetty 9.4.14 as Embedded Server #3219

0Steve0 opened this issue Dec 20, 2018 · 12 comments

Comments

@0Steve0
Copy link

0Steve0 commented Dec 20, 2018

I am using jetty as a embedded Server in eclipse. It keep throwing ClassNotFoundException when start the server.

Stack track looks like:
Failed startup of context o.e.j.w.WebAppContext@6561e97d{/,null,UNAVAILABLE}
java.lang.ClassNotFoundException: org.eclipse.jetty.webapp.WebInfConfiguration
at java.net.URLClassLoader.findClass(URLClassLoader.java:609)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:925)
at java.lang.ClassLoader.loadClass(ClassLoader.java:870)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:343)
at java.lang.ClassLoader.loadClass(ClassLoader.java:853)
at org.eclipse.osgi.internal.framework.ContextFinder.loadClass(ContextFinder.java:132)
at java.lang.ClassLoader.loadClass(ClassLoader.java:853)
at org.eclipse.jetty.util.Loader.loadClass(Loader.java:65)
at org.eclipse.jetty.webapp.WebAppContext.loadConfigurations(WebAppContext.java:1035)
at org.eclipse.jetty.webapp.WebAppContext.preConfigure(WebAppContext.java:473)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:544)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.server.Server.start(Server.java:419)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.Server.doStart(Server.java:386)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)

My code is quite simple:
Server server = new Server(8080)
WebAppContext context = new WebAppContext();
..........
context.addServlet(xxxx);
............
server.start();

@0Steve0
Copy link
Author

0Steve0 commented Dec 20, 2018

I tried different versions of jetty. It does not work on jetty 9.4.10 either but works fine without any exception on jetty 9.3.8.

@sbordet
Copy link
Contributor

sbordet commented Dec 20, 2018

@0Steve0 dude, you did not even provide a stack trace, nor the name of the class that was not found.
99% is a missing jar on your side, so please try harder on your side first and if you still have a problem provide detailed information.

@0Steve0
Copy link
Author

0Steve0 commented Dec 20, 2018

@sbordet sorry mate, forgot to update my description.

@sbordet
Copy link
Contributor

sbordet commented Dec 20, 2018

@0Steve0 what is your classpath? List at least Jetty's jars.

@joakime
Copy link
Contributor

joakime commented Dec 20, 2018

You obviously have a strange environment.

Your environment obviously loaded org.eclipse.jetty.webapp.WebAppContext, but couldn't load org.eclipse.jetty.webapp.WebInfConfiguration from the same jetty-webapp-<version>.jar??

The only time I've seen this is when using OSGi and/or an IBM JVM.

@0Steve0
Copy link
Author

0Steve0 commented Dec 20, 2018

@joakime you are right, i am using OSGI

@0Steve0
Copy link
Author

0Steve0 commented Dec 20, 2018

I found a similar issue here: #705.

@joakime
Copy link
Contributor

joakime commented Dec 20, 2018

@0Steve0 make sure you have and use Apache SpiFly with your OSGi environment. (it's a requirement for ServiceLoader functionality)

@joakime
Copy link
Contributor

joakime commented Dec 20, 2018

@0Steve0 also, you'll have nearly no luck using standard techniques and behaviors with OSGi.

The entire jetty-osgi tree exists to work around the deficiencies and bad assumptions in OSGi.

See: https://github.com/eclipse/jetty.project/tree/jetty-9.4.x/jetty-osgi

Probably need to start at jetty-osgi-boot .
If you want to use JSP, you have different OSGi hack you need to use.
If you want to use ALPN and HTTP/2 you have a different OSGi hack you need to use.
If you want annotations to work, you have a different OSGi hack to use.

In short, you'll have a tremendous amount of boilerplate code to "setup" of the OSGi environment, along with special one-off classes of existing Jetty classes you'll need to use just because of your choice of OSGi.

@0Steve0
Copy link
Author

0Steve0 commented Dec 20, 2018

@joakime By reading through #705, i found a potential fix for this issue, just add Thread.currentThread().setContextClassLoader(MY_SERVER_CLASS.class.getClassLoader()); before server.Start();

@0Steve0 0Steve0 closed this as completed Jan 7, 2019
@xipki
Copy link

xipki commented Aug 1, 2021

Since BouncyCastle version 1.69, you also need to copy the bcutil-<version>.jar to lib/ext.

@janbartel
Copy link
Contributor

@0Steve0 OSGi classloading is completely different from standard jvm classloading. That is why we provide a bunch of helper osgi bundles to make webapp classloading work in this environment which are discussed here: https://www.eclipse.org/jetty/documentation/jetty-9/index.html#framework-jetty-osgi.

Explicltly setting the context classloader as the classloader that loaded some particular class may happen solve some of your classloading issues but is not a generic solution with OSGi. I strongly encourage you to deploy the the jetty-osgi-boot (and jetty-osgi-boot-jsp jars if you are using jsps).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants