Eclipse - 将 .jar 添加到动态 Web 项目 [英] Eclipse - add .jar to Dynamic Web Project
问题描述
当我在 eclipse Dynamic Web Project
中使用以下代码时 servlet
,就像这样:
When I use the follow code in eclipse Dynamic Web Project
inside servlet
, like this :
@WebServlet("/CreateCustomerServlet")
public class CreateCustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private void test() throws SQLException, ClassNotFoundException {
// use the JAR ...
Class.forName("com.mysql.jdbc.Driver");//here the exception
}
}
它抛出异常:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
在一行中:
Class.forName("com.mysql.jdbc.Driver");
在动态 web 项目的构建路径中,我添加了 jar:
In the Build path of the dynamic web project I added the jar:
mysql-connector-java-5.1.21-bin.jar
我添加了文件夹的快照,你可以看到它在哪里需要的jar -
I add snapshot of the folder , you can see where it is the required jar -
推荐答案
如您的屏幕截图所示,mysql jar 没有出现在 Web App Libraries 节点下.将jar粘贴到WebContent/WEB-INF/lib
中,它会自动添加到构建路径和
到webapp的运行时类路径(并且会出现在Web 应用程序库).
As your screenshot shows, the mysql jar doesn't appear under the Web App Libraries node. Paste the jar in WebContent/WEB-INF/lib
, and it will be automatically added to the build path and
to the runtime classpath of the webapp (and it will appear under Web App Libraries).
这篇关于Eclipse - 将 .jar 添加到动态 Web 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!