开发 Web 应用程序时,我应该将外部 JAR 文件放在哪里? [英] Where should I put external JAR files when developing a web app?

查看:24
本文介绍了开发 Web 应用程序时,我应该将外部 JAR 文件放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Eclipse 中使用 Java (Servlets/JSP) 开发动态 Web 应用程序.我正在尝试使用外部 JAR(我使用的是来自 Apache Commons 的 StringUtils),但我不知道该将 jar 放在哪里(/lib/WEB-INF/lib?) 以及我需要如何配置我的类路径(在 Eclipse 中).

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).

我尝试将 JAR 放在上述两个位置,并通过在项目属性中单击 Add JAR 将它们加载到类路径,并且两个解决方案都可以正常编译,但是会出现如下运行时错误:

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:

严重:servlet UserList 的 Servlet.service() 抛出异常java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils在 cs236369.hw5.db.MySqlDbHandler.insert(MySqlDbHandler.java:58)在 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)在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)在 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)在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)在 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(Unknown Source)

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)

那么……我如何加载外部 JAR?

So... how do I load external JARs?

请注意,我不太熟悉如何在 Java、VM 中加载外部 JAR,或者 Eclipse 如何管理这一切,所以我希望得到详细的解决方案.

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.

推荐答案

它们必须放在 yourapp/WEB-INF/lib 中.(在 Eclipse 中,您正在正确添加它们).所以要确保罐子真的在那里.请注意,如果您从 eclipse 中启动应用程序,则必须配置部署组装.

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.

部署程序集是通过右键单击>配置的属性 >在 Eclipse 中部署程序集.在那里你应该添加"你的Java构建路径条目"

The deployment assembly is configured through right click > properties > deployment assembly in eclipse. There you should "Add" your "Java build path entries"

这篇关于开发 Web 应用程序时,我应该将外部 JAR 文件放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆