ClassNotFoundException when using User Libraries in Eclipse build path
Asked Answered
G

2

42

I'm using Eclipse 3.7 (STS) with Tomcat 7 running inside the IDE. I've created a new Dynamic Web project and added a single JSP file to the web content root folder. I can run Tomcat and access the JSP from within Eclipse with no problems.

I've added a few 3rd party JAR's to the project from User Libraries (I'm not using maven or auto dependecies managment). In the JSP I reference a class from the project's JAR file, I can compile this with no problem, but when I deploy on Tomcat the JSP throws ClassNotFoundException. Clearly, Tomcat can't find the JAR's from my library settings. I tried creating a Run As configuration for Tomcat Server and I set the classpath to match the classpath settings of the project, but I still get the same classnotfound problem.

I could get around the issue by manually copying all project JARs to the WEB-INF/lib directory so the webapp can find all dependencies, but that's absurd and I don't expect that to be the solution since it's a maintenance nightmare.

Am I missing something?

Gaming answered 10/8, 2011 at 3:44 Comment(0)
S
99

In project's properties, go to Deployment Assembly. Add there the buildpath entries as well which you've manually added as user libraries. It'll end up in /WEB-INF/lib of the deployed WAR.

enter image description here

See also:

Syverson answered 10/8, 2011 at 4:21 Comment(7)
LIFESAVER! That worked, I knew there had to be an easy way, jesus christ, now I can get some sleep, thanks a million!Gaming
Just in case you are using an older eclipse, in version 3.4.2 the name of the option in project properties is 'Java EE Module Dependencies'Remus
@bizmark: That's correct. See also stackoverflow.com/questions/3511479/…Syverson
This information should be in every tutorial on dynamic projectsMalta
@BalusC-please have a look at my problem stackoverflow.com/questions/18249117/…Lemur
This doesn't seem to work so well for native libraries (JNI): it starts fine once, but if you change any code, Tomcat tries to reload the library and you get an error like java.lang.UnsatisfiedLinkError: Native Library /usr/local/lib/libgdalconstjni.so already loaded in another classloader. Is there a way to tag it to only be loaded once? Setting the user library to be a system library didn't help.Consume
@Syverson thanks alot man. You saved my alot of time. I were searching out for past two days. You answer resolved my question.Herries
S
4

You'll need to copy the jar files to the WEB-INF/lib folder: that is where they are supposed to be.

Eclipse should offer you the option of generating a WAR file that includes all the dependencies: I haven't used Web Tools for a good while but one way or another all dependencies have to be in WEB-INF/lib or the class loader won't be able to find them.

Showcase answered 10/8, 2011 at 4:10 Comment(1)
I can't find any WAR settings for Dynamic Web projects in Eclipse STS, but I know exactly what you're talking about because that's what I expected after using Eclipse Galileo for years.Gaming

© 2022 - 2024 — McMap. All rights reserved.