Eclipse - 将.jar添加到动态 Web 项目

2022-09-01 18:09:35

当我在eclipse里面使用下面的代码时,就像这样:Dynamic Web Projectservlet

@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:

mysql-connector-java-5.1.21-bin.jar

我添加了文件夹的快照,您可以看到它在哪里是所需的jar -

enter image description here


答案 1

如屏幕截图所示,mysql jar 不会显示在“Web 应用库”节点下。将 jar 粘贴到 中,它将自动添加到 webapp 的运行时类路径的构建路径中(并且它将出现在 Web App 库下)。WebContent/WEB-INF/liband


答案 2

刚才我解决了这个问题。

  • 只需转到您的项目。properties
  • 选择 -> -> 。Deployment AssemblyaddJava Build Path Entries

你完成了!


推荐