Java ClassNotFoundException com.mysql.jdbc.Driver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1585811/
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
ClassNotFoundException com.mysql.jdbc.Driver
提问by Dusk
This question might have asked here number of times . After doing some google search for the above error and doing some update, I can't understand why I'm still getting that error. I've already put my driver-- mysql-connector-java-5.1.5-bin in the classpath:
这个问题可能在这里问过多次。在对上述错误进行了一些谷歌搜索并进行了一些更新后,我不明白为什么我仍然收到该错误。我已经把我的驱动程序——mysql-connector-java-5.1.5-bin 放在了类路径中:
Java_Home\jre\lib\
Java_Home\jre\lib\ext\
Java_Home\lib
and the code which I'm using to connect to mysql database is:
我用来连接到 mysql 数据库的代码是:
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mail","root","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select message_body from deadletter");
String dbtime;
while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
}
con.close();
}
catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
}
}
and the complete stacktrace of the above exception is:
并且上述异常的完整堆栈跟踪是:
java.lang.ClassNotFoundException: com.mysql.jdbc:Driver
at java.net.URLClassLoader.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at mail.main(mail.java:114)
Now, what's wrong I'm doing here?
现在,我在这里做什么错了?
采纳答案by Bostone
The most common cause is that you have some conflict in where your classes are loaded from. For example if you have 2 locations and one has JDBC drivers and the other one not then if your classloader loads from the 1st location and some class from the 1st location wants to use the driver - the driver is not there. So look for the duplicate JARs that are using your driver
最常见的原因是您在加载类的位置存在一些冲突。例如,如果您有 2 个位置,一个有 JDBC 驱动程序,而另一个没有,那么如果您的类加载器从第一个位置加载,并且第一个位置的某个类想要使用该驱动程序 - 驱动程序不在那里。因此,请查找使用您的驱动程序的重复 JAR
回答by WillfulWizard
I keep the mysql-connector jar with my project rather than in Javahome. As a result, you can be sure it can be found by being sure its in the local classpath. A big upside is that you you can more the project to another machine and not have to worry about (or forget) to set this up again. I personally like including it in version control.
我将 mysql-connector jar 保存在我的项目中,而不是在 Javahome 中。因此,您可以通过确定它在本地类路径中来确定可以找到它。一个很大的好处是您可以将项目更多地转移到另一台机器上,而不必担心(或忘记)再次设置它。我个人喜欢将它包含在版本控制中。
回答by Pascal Thivent
What did you put exactly in lib
, jre/lib
or jre/lib/ext
? Was it the jar mysql-connector-java-5.1.5-bin.jar
or something else (like a directory)?
你到底放了什么lib
,jre/lib
或者jre/lib/ext
?是 jarmysql-connector-java-5.1.5-bin.jar
还是其他东西(比如目录)?
By the way, I wouldn't put it in lib
, jre/lib
or jre/lib/ext
, there are other ways to add a jar to the classpath. You can do that by adding it explicitly the CLASSPATH environment variable. Or you can use the -cp
option of java
. But this is another story.
顺便说一句,我不会把它放在lib
,jre/lib
或者jre/lib/ext
,还有其他方法可以将 jar 添加到类路径中。您可以通过将其显式添加到 CLASSPATH 环境变量来实现。或者您可以使用 的-cp
选项java
。但这是另一个故事。
回答by ismail shepl
I have the same problem but I found this after a long search: http://www.herongyang.com/JDBC/MySQL-JDBC-Driver-Load-Class.html
我有同样的问题,但经过长时间的搜索我发现了这个:http: //www.herongyang.com/JDBC/MySQL-JDBC-Driver-Load-Class.html
But I made some change. I put the driver in the same folder as my ConTest.java
file,
and compile it, resulting in ConTest.class
.
但我做了一些改变。我将驱动程序放在与我的ConTest.java
文件相同的文件夹中,然后编译它,结果是ConTest.class
.
So in this folder have
所以在这个文件夹中有
ConTest.class
mysql-connector-java-5.1.14-bin.jar
and I write this
我写这个
java -cp .;mysql-connector-java-5.1.14-bin.jar ConTest
This way if you not use any IDE just cmd in windows or shell in linux.
这样,如果您不使用任何 IDE,仅在 windows 中使用 cmd 或在 linux 中使用 shell。
回答by Ruthreshwar
I too struggled with the same problem and finally got the solution for it.
Just copy the MySql-Connector.jar
into Tomcat's lib
folder, and then remove the jar from the webapp's lib
folder, and then, run the project.
我也遇到了同样的问题,终于找到了解决方案。只需将其复制MySql-Connector.jar
到Tomcat的lib
文件夹中,然后从webapp的lib
文件夹中删除jar ,然后运行该项目。
回答by Luillyfe
Ok, i find solution changing the path of mysql-conector-java.jar to the follow path:
好的,我找到了将 mysql-conector-java.jar 的路径更改为以下路径的解决方案:
ProjectName/WebContent/Web-inf/lib/mysql-conector-java.jar
So you need to add the conector to project again and delete the previous one.
所以你需要再次将连接器添加到项目中并删除之前的连接器。
回答by user3619178
Ok..May be i can also contribute my solution.. Right click on project -properties -->Deployment assembly...there you need to add the mysql-connector-java.jar and apply it...which makes your prject configured with web-Libraries that has this sql connector...This worked for me.. I hope this works for you guys as well
好吧..也许我也可以贡献我的解决方案.. 右键单击项目 -properties --> 部署程序集...在那里你需要添加 mysql-connector-java.jar 并应用它...这使你的项目配置了具有此 sql 连接器的网络库...这对我有用...我希望这对你们也有用
回答by Danysh Mushtaq
see to it that the argument of Class. forName method is exactly "com. mysql. jdbc. Driver". If yes then see to it that mysql connector is present in lib folder of the project. if yes then see to it that the same mysql connector . jar file is in the ClassPath variable (system variable)..
确保 Class 的参数。forName 方法正是“com.mysql.jdbc.Driver”。如果是,则确保项目的 lib 文件夹中存在 mysql 连接器。如果是,那么请确保使用相同的 mysql 连接器。jar 文件位于 ClassPath 变量(系统变量)中。
回答by CommonSenseCode
The only thing that worked for me was downloading the mysql-connector-java-5.0.8-bin.jardirectly from the MySQL website:
唯一对我有用的是直接从 MySQL 网站下载mysql-connector-java-5.0.8-bin.jar:
http://dev.mysql.com/downloads/connector/j/3.1.html
http://dev.mysql.com/downloads/connector/j/3.1.html
Then if you're using Eclipse paste this mysql-connector-java-5.0.8-bin.jarin the WEB-INF lib folder
然后,如果您使用 Eclipse,请将此mysql-connector-java-5.0.8-bin.jar粘贴 到 WEB-INF lib 文件夹中
If doesn't works try with any of the solutions posted in this link: http://javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html
如果不起作用,请尝试使用此链接中发布的任何解决方案:http: //javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html
回答by Kenneth Key
If you're facing this problem with Eclipse, I've been following many different solutions but the one that worked for me is this:
如果您在使用 Eclipse 时遇到此问题,我一直在遵循许多不同的解决方案,但对我有用的解决方案是:
Right click your project folder and open up Properties.
From the right panel, select Java Build Path then go to Libraries tab.
Select Add External JARs to import the mysql driver.
From the right panel, select Deployment Assembly.
Select Add..., then select Java Build Path Entries and click Next.
You should see the sql driver on the list. Select it and click first.
右键单击您的项目文件夹并打开属性。
从右侧面板中,选择 Java Build Path,然后转到 Libraries 选项卡。
选择添加外部 JAR 以导入 mysql 驱动程序。
从右侧面板中,选择部署程序集。
选择 Add...,然后选择 Java Build Path Entries 并单击 Next。
您应该会在列表中看到 sql 驱动程序。选择它并首先单击。
And that's it! Try to run it again! Cheers!
就是这样!尝试再次运行它!干杯!