在 Eclipse 构建路径中使用用户库时出现 ClassNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7005644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ClassNotFoundException when using User Libraries in Eclipse build path
提问by user646584
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.
我将 Eclipse 3.7 (STS) 与在 IDE 中运行的 Tomcat 7 一起使用。我创建了一个新的动态 Web 项目并将一个 JSP 文件添加到 Web 内容根文件夹。我可以毫无问题地运行 Tomcat 并从 Eclipse 中访问 JSP。
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.
我从用户库中向项目添加了一些 3rd 方 JAR(我没有使用 maven 或自动依赖管理)。在 JSP 中,我从项目的 JAR 文件中引用了一个类,我可以毫无问题地编译它,但是当我在 Tomcat 上部署时,JSP 会抛出 ClassNotFoundException。显然,Tomcat 无法从我的库设置中找到 JAR。我尝试为 Tomcat 服务器创建运行方式配置,并将类路径设置为与项目的类路径设置相匹配,但我仍然遇到相同的 classnotfound 问题。
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.
我可以通过手动将所有项目 JAR 复制到 WEB-INF/lib 目录来解决这个问题,这样 webapp 就可以找到所有依赖项,但这是荒谬的,我不希望这是解决方案,因为它是维护噩梦。
Am I missing something?
我错过了什么吗?
回答by BalusC
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.
在项目的属性中,转到Deployment Assembly。在那里添加构建路径条目以及您手动添加为用户库的条目。它将在/WEB-INF/lib
已部署的 WAR 中结束。
回答by Femi
You'll need to copy the jar files to the WEB-INF/lib folder: that is where they are supposed to be.
您需要将 jar 文件复制到 WEB-INF/lib 文件夹:它们应该在那里。
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.
Eclipse 应该为您提供生成包含所有依赖项的 WAR 文件的选项:我有一段时间没有使用 Web 工具,但是一种或另一种方式所有依赖项都必须在 WEB-INF/lib 中,否则类加载器将不会无法找到它们。