如何在 Eclipse web 项目中安装 JDBC 驱动程序而不面对 java.lang.ClassNotFoundexception

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2353141/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:14:09  来源:igfitidea点击:

How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception

eclipsejakarta-eejdbcclassnotfoundexception

提问by Kiril

There is a VERY similar questionto mine but in my case I don't have any duplicate jars in my build path, so the solution does not work for me. I've searched google for a couple of hours now, but none of the solutions I've found there actually resolve my issue. I'm creating a web site with some database connectivity for a homework. I'm using a MySQL database, developing in Eclipse and running on Windows.

有一个与非常相似的问题,但就我而言,我的构建路径中没有任何重复的 jar,因此该解决方案对我不起作用。我已经在谷歌上搜索了几个小时,但我在那里找到的解决方案都没有真正解决我的问题。我正在为家庭作业创建一个具有一些数据库连接的网站。我正在使用 MySQL 数据库,在 Eclipse 中开发并在 Windows 上运行。

I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driverwith the following code:

我不断收到java.lang.ClassNotFoundException: com.mysql.jdbc.Driver以下代码:

import java.sql.*;
//...
public void someMethodInMyServlet(PrintWriter out)
{
    Connection connection = null;
    PreparedStatement query = null;
    try {

        out.println("Create the driver instance.<br>");
        Class.forName("com.mysql.jdbc.Driver").newInstance();

        out.println("Get the connection.<br>");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
        query = connection.prepareStatement( "SELECT * FROM customers");

        //...
    } catch (Exception e)
    {
        out.println(e.toString()+"<br>");
    }
}
//...

When I run the above code I get the following output:

当我运行上面的代码时,我得到以下输出:

Create the driver instance.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

It doesn't get past the Class.forName...line and I can't figure out why! Here is what I did:

它没有越过Class.forName...界线,我不知道为什么!这是我所做的:

  1. Download mysql-connector.
  2. Put it in my MySQL folder C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar.
  3. Opened the project properties in Eclipse.
  4. Add External Jar to my Build Path and I selected mysql-connector-java-5.1.12-bin.jar.
  1. 下载 mysql 连接器。
  2. 把它放在我的 MySQL 文件夹中C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar
  3. 在 Eclipse 中打开项目属性。
  4. 将外部 Jar 添加到我的构建路径,然后我选择了mysql-connector-java-5.1.12-bin.jar.

Every time I attempt to use the servlet I get the same error regardless if I have the jar in there or if I don't. Could you help me figure this out?

每次我尝试使用 servlet 时,我都会收到相同的错误,无论我在那里有 jar 还是没有。你能帮我解决这个问题吗?

回答by BalusC

As for every "3rd-party" library in flavor of a JAR file which is to be used by the webapp, just copy/drop the physical JAR file in webapp's /WEB-INF/lib. It will then be available in webapp's default classpath. Also, Eclipse is smart enough to notice that. No need to hassle with buildpath. However, make sure to remove all unnecessary references you added before, else it might collide.

对于每个由 webapp 使用的 JAR 文件风格的“3rd-party”库,只需将物理 JAR 文件复制/放到 webapp 的/WEB-INF/lib. 然后它将在 webapp 的默认类路径中可用。此外,Eclipse 足够聪明,可以注意到这一点。无需麻烦构建路径。但是,请确保删除您之前添加的所有不必要的引用,否则可能会发生冲突。

An alternative is to install it in the server itself by dropping the physical JAR file in server's own /libfolder. This is required when you're using server-provided JDBC connection pool data source which in turn needs the MySQL JDBC driver.

另一种方法是通过将物理 JAR 文件放在服务器自己的/lib文件夹中来将其安装在服务器本身中。当您使用服务器提供的 JDBC 连接池数据源而需要 MySQL JDBC 驱动程序时,这是必需的。

See also:

也可以看看:

回答by DJ.

Since you are running it in servlet, you need to have the jar accessible by the servlet container. You either include the connector as part of your application waror put it as part of the servlet container's extended library and datasource management stuff, if it has one. The second part is totally depend on the container that you have.

由于您正在运行它servlet,您需要让servlet 容器可以访问该 jar 。您可以将连接器作为应用程序的一部分包含在内,war或者将其作为 servlet 容器的扩展库和数据源管理内容的一部分(如果有的话)。第二部分完全取决于您拥有的容器。

回答by trashgod

The others are right about making the driver JAR available to your servlet container. My comment was meant to suggest that you verify from the command line whether the driver itself is intact.

其他关于使驱动程序 JAR 可用于您的 servlet 容器是正确的。我的评论旨在建议您从命令行验证驱动程序本身是否完好无损。

Rather than an empty main(), try something like this, adapted from the included documentation:

而不是一个空的main(),尝试这样的事情,改编自包含的文档:

public class LoadDriver {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
    }
}

On my platform, I'd do this:

在我的平台上,我会这样做:

$ ls mysql-connector-java-5.1.12-bin.jar 
mysql-connector-java-5.1.12-bin.jar
$ javac LoadDriver.java 
$ java -cp mysql-connector-java-5.1.12-bin.jar:. LoadDriver

On your platform, you need to use ;as the path separator, as discussed hereand here.

在您的平台上,您需要将其;用作路径分隔符,如此此处所述

回答by Satya

Place mysql-connector-java-5.1.6-bin.jar to the \Apache Tomcat 6.0.18\lib folder. Your problem will be solved.

将 mysql-connector-java-5.1.6-bin.jar 放到 \Apache Tomcat 6.0.18\lib 文件夹中。您的问题将得到解决。

回答by Rajat

Just follow these steps:

只需按照以下步骤操作:

1) Install eclipse
2) Import Apache to eclipse
3) Install mysql
4) Download mysqlconnector/J
5) Unzip the zipped file navigate through it until you get the bin file in it. Then place all files that are present in the folder containing bin to C:\Program Files\mysql\mysql server5.1/ then give the same path as the address while defining the driver in eclipse.

1) 安装 eclipse
2) 将 Apache 导入到 eclipse
3) 安装 mysql
4) 下载 mysqlconnector/J
5) 解压缩压缩文件,浏览它,直到你得到 bin 文件。然后将包含bin的文件夹中存在的所有文件放在C:\Program Files\mysql\mysql server5.1/然后在eclipse中定义驱动程序时给出与地址相同的路径。

That's all very easy guys.

这都是很容易的家伙。

回答by oak

What you should not do do (especially when working on a shared project)

您不应该做的事情(尤其是在处理共享项目时)

Ok, after had the same issue and after reading some answers here and other places. it seems that putting external libinto WEB-INF/libis not that good idea as it pollute webapp/JRE libs with server-specific libraries- for more information check this answer"

好的,在遇到同样的问题并在这里和其他地方阅读了一些答案之后。似乎把external libWEB-INF/lib是不是好主意,因为它pollute webapp/JRE libs with server-specific libraries- 的更多信息,请这个答案”

Another solution that i do NOT recommend is: to copy it into tomcat/lib folder. although this may work, it will be hard to manage dependency for a shared(git for example) project.

我不推荐的另一个解决方案是:将其复制到 tomcat/lib 文件夹中。尽管这可能有效,但很难管理共享(例如 git)项目的依赖项。

Good solution 1

好的解决方案 1

Create vendorfolder. put there all your external lib. then, map this folder as dependency to your project. in eclipse you need to

创建vendor文件夹。把你所有的外部库放在那里。然后,将此文件夹作为依赖项映射到您的项目。在日食中你需要

  1. add your folder to the build path
    1. Project Properties-> Java build path
    2. Libraries-> add external lib or any other solution to add your files/folder
  2. add your build path to deployment Assembly(reference)
    1. Project Properties-> Deployment Assembly
    2. Add-> Java Build Path Entries
    3. You should now see the list of libraries on your build path that you can specify for inclusion into your finished WAR.
    4. Select the ones you want and hit Finish.
  1. 将您的文件夹添加到 build path
    1. Project Properties-> Java build path
    2. Libraries-> 添加外部库或任何其他解决方案来添加您的文件/文件夹
  2. 将您的构建路径添加到deployment Assembly参考
    1. Project Properties-> Deployment Assembly
    2. Add-> Java Build Path Entries
    3. 您现在应该会看到构建路径上的库列表,您可以指定这些库包含在完成的 WAR 中。
    4. 选择你想要的,然后点击完成。

Good solution 2

好的解决方案2

Use maven(or any alternative) to manage project dependency

使用maven(或任何替代方法)管理项目依赖

回答by Rutvik patel

Put mysql-connector-java-5.1.38-bin.jar to the C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib folder.by doing this program with execute

将 mysql-connector-java-5.1.38-bin.jar 放到 C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib 文件夹中。通过执行这个程序

回答by Anand Rockzz

My issue was a little different. Instead of jdbc:oracle:thin:@server:port/servicei had it as server:port/service.

我的问题有点不同。而不是jdbc:oracle:thin:@server:port/service我把它作为server:port/service.

Missing was jdbc:oracle:thin:@in urlattribute in GlobalNamingResources.Resource. But I overlooked tomcat exception's

GlobalNamingResources.Resourcejdbc:oracle:thin:@中的url属性缺失。但我忽略了 tomcat 异常的

java.sql.SQLException: Cannot create JDBC driver of class 'oracle.jdbc.driver.OracleDriver' for connect URL 'server:port/service'

回答by Kingz

assuming your project is maven based, add it to your POM:

假设您的项目是基于 maven 的,请将其添加到您的 POM 中:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.26</version>
    </dependency>

Save > Build > and test connection again. It works! Your actual mysql java connector version may vary.

保存 > 构建 > 并再次测试连接。有用!您的实际 mysql java 连接器版本可能会有所不同。

回答by Praveen

If the problem still persists,

如果问题仍然存在,

Put the- mysql-connector-java-5.0.8-bin jar in a place inside your Tomcat->lib->folder (No matter where you've installed your Tomcat). And change your environmental variable (Done by clicking Properties of Mycomputer -Advanced system settings- Environmental variables-And set a new variable name & variable values as the place where your lib file resides.Dont forget to enter a ; at the end of the path)

将 - mysql-connector-java-5.0.8-bin jar 放在 Tomcat->lib-> 文件夹内的某个位置(无论您将 Tomcat 安装在何处)。并更改您的环境变量(通过单击我的计算机的属性 - 高级系统设置 - 环境变量 - 并设置新的变量名称和变量值作为您的 lib 文件所在的位置。不要忘记在路径末尾输入一个 ; )

If still problem persists Try downloading commons-collections-2.0.jar (http://www.docjar.com/jar_detail/commons-collections-2.0.jar.html) and paste the jar in the same place where your mysql jar resides (ie) inside Tomcat-lib.

如果问题仍然存在尝试下载 commons-collections-2.0.jar ( http://www.docjar.com/jar_detail/commons-collections-2.0.jar.html) 并将 jar 粘贴到您的 mysql jar 所在的同一位置 (即)在 Tomcat-lib 中。

Clean your project-Stop your server- Finally try to run.

清理你的项目 - 停止你的服务器 - 最后尝试运行。