eclipse 开发 Web 应用程序时,我应该将外部 JAR 文件放在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6496560/
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
Where should I put external JAR files when developing a web app?
提问by Amir Rachum
I'm developing a dynamic web application with Java (Servlets/JSP) in Eclipse. I'm trying to use an external JAR (I'm using StringUtils
from Apache Commons) and I'm confused as to where I should put the jar (/lib
, /WEB-INF/lib
?) and how do I need to configure my class path (in Eclipse).
我正在 Eclipse 中使用 Java (Servlets/JSP) 开发动态 Web 应用程序。我正在尝试使用外部 JAR(我StringUtils
从 Apache Commons 使用),但我不知道应该将 jar 放在哪里(/lib
,/WEB-INF/lib
?)以及我需要如何配置我的类路径(在 Eclipse 中)。
I tried putting the JARs in both of the aforementioned places, and loading them to the classpath by clicking Add JAR
in the project properties and both solution compile fine, but give a runtime error like so:
我尝试将 JAR 放在上述两个位置,并通过单击Add JAR
项目属性将它们加载到类路径,两个解决方案都可以正常编译,但会出现如下运行时错误:
SEVERE: Servlet.service() for servlet UserList threw exception java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils at cs236369.hw5.db.MySqlDbHandler.insert(MySqlDbHandler.java:58) at cs236369.hw5.servlets.UserList.doGet(UserList.java:50) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)
严重:servlet UserList 的 Servlet.service() 抛出异常 java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils at cs236369.hw5.db.MySqlDbHandler.insert(MySqlDbHandler.java:58) at cs236369.hw5.servlets .UserList.doGet(UserList.java:50) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 在 org.apache。 catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java: 233) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 在 org.apache.catalina.core.StandardHostValve。invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org. .catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol) .java:588) 在 org.apache.tomcat.util.net.JioEndpoint$Worker.run(JioEndpoint.java:489) 在 java.lang.Thread.run(未知来源)CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)在 org.apache.tomcat.util.net.JioEndpoint$Worker.run(JioEndpoint.java:489) 在 java.lang.Thread.run(未知来源)CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)在 org.apache.tomcat.util.net.JioEndpoint$Worker.run(JioEndpoint.java:489) 在 java.lang.Thread.run(未知来源)
So... how do I load external JARs?
那么...我如何加载外部 JAR 文件?
Please take note that I am not that familiar with how external JARs are loaded in Java, the VM, or how Eclipse manages it all, so I would appreciate detailed solutions.
请注意,我不太熟悉如何在 Java、VM 中加载外部 JAR,或者 Eclipse 如何管理这一切,所以我希望得到详细的解决方案。
回答by Bozho
They must be placed in yourapp/WEB-INF/lib
. (In eclipse you are adding them properly). So make sure the jar is really there. Note that if you are starting the app from within eclipse, you would have to configure deployment assembling.
它们必须放在yourapp/WEB-INF/lib
. (在 Eclipse 中,您正在正确添加它们)。所以要确保罐子真的在那里。请注意,如果您从 eclipse 中启动应用程序,则必须配置部署组装。
The deployment assembly is configured through right click > properties > deployment assembly
in eclipse. There you should "Add" your "Java build path entries"
部署程序集是通过right click > properties > deployment assembly
在 eclipse 中配置的。在那里你应该“添加”你的“Java构建路径条目”