java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 错误即使在导入库后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37254131/
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
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error even after importing library
提问by Muhammad Faizan Ul Haq
I have imported my library "mysql-connector-java-5.1.39" as answered by most people to exact same question , but I am still getting this error
我已经导入了我的库“mysql-connector-java-5.1.39”,正如大多数人回答的完全相同的问题,但我仍然收到此错误
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
here is the code of test class
这是测试类的代码
package database;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
getConnection();
}
public static Connection getConnection() throws Exception {
try{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String username="root";
String password="root";
Class.forName(driver);
Connection conn= DriverManager.getConnection(url,username,password);
System.out.println("connected");
return conn;
}
catch (Exception e){
System.out.println(e);
}
return null;
}
}
Using mac OS .
使用 mac 操作系统。
采纳答案by Stephen C
Everybody repeat after me. ( :-) )
大家跟着我重复一遍。(:-))
"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" is NOT a compilation error.
“java.lang.ClassNotFoundException: com.mysql.jdbc.Driver”不是编译错误。
Therefore, changing the >>build<< path or adding an import
cannot fix the problem.
因此,更改>>build<< 路径或添加一个import
不能解决问题。
The solution is to make sure that the JAR file is on the classpath when you run the application. For example, if your test class is in bin\database\Main.class
and the driver JAR is in lib
...
解决方案是确保运行应用程序时 JAR 文件位于类路径中。例如,如果您的测试类在bin\database\Main.class
,而驱动程序 JAR 在lib
...
$ java -classpath bin:lib/mysql-connector-java-5.1.39.jar database.Main
回答by Naman
If the jar is already added to your external libs, you can simply add :
如果 jar 已经添加到您的外部库中,您只需添加:
import com.mysql.jdbc.Driver;
and it shall work in your class.
它会在你的课堂上工作。
This shall help further SO-21580499
这将有助于进一步SO-21580499
回答by MeisterAjit
Some of the most possible reasons of "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" in your code are:
代码中“java.lang.ClassNotFoundException: com.mysql.jdbc.Driver”的一些最可能的原因是:
- You don't have mysql-connector.jar in your Classpath.
- mysql-connector.jar is in your classpath but somehow your classpath is getting overridden.
- mysql-connector.jar is in classpath but current user doesn't have read permission.
- 您的类路径中没有 mysql-connector.jar。
- mysql-connector.jar 在您的类路径中,但不知何故您的类路径被覆盖了。
- mysql-connector.jar 在类路径中,但当前用户没有读取权限。
回答by Slava Imeshev
It looks like you app cannot find the JDBC driver for Microsoft SQL server. You can download it below and add the jar to your WEB-INF/lib:
看起来您的应用程序找不到 Microsoft SQL 服务器的 JDBC 驱动程序。您可以在下面下载它并将 jar 添加到您的 WEB-INF/lib:
https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
回答by Neeraj Kumar
Maybe you are missing any one of the following things : Both of the following things are necessary to remove the error :
也许您缺少以下任何一项:以下两项都是消除错误所必需的:
Adding mysql-connector.jar file to your WebContent / WEB-INF / libfolder.
Adding mysql-connector.jar file in your classpath
将 mysql-connector.jar 文件添加到您的WebContent/WEB-INF/lib文件夹。
在类路径中添加 mysql-connector.jar 文件
Download mysql-connector.jar from https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.11
从https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.11下载 mysql-connector.jar
For solving first problem paste the above downloaded jar file into your project's WebContent / WEB-INF / libfolder.
为了解决第一个问题,将上面下载的 jar 文件粘贴到项目的WebContent / WEB-INF / lib文件夹中。
For Solving second problem - right click on your project , go to propertiesthen go to Java Build Pathand then in Librariessectionchoose Add External JARsand select the above downloaded jar file.
对于解决第二个问题 - 右键单击您的项目,转到属性,然后转到Java 构建路径,然后在库部分中选择添加外部 JAR并选择上面下载的 jar 文件。
Hope it helps.
希望能帮助到你。