java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 运行 jar 文件时

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

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver When run jar file

java

提问by user1024858

Possible Duplicate:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

可能的重复:
java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

Hi all,
here is my sample code:

大家好,
这是我的示例代码:

Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; 
String password = "admin";
try {
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  conn.close();
  System.out.println("Disconnected from database");
} catch (Exception e) {
  e.printStackTrace();
}

I run in eclipse it's ok, but i built to jar file and run on command line java -jar Test.jari get this error:

我在 Eclipse 中运行它没问题,但是我构建到 jar 文件并在命令行上运行java -jar Test.jar我收到此错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader.run(Unknown Source)
        at java.net.URLClassLoader.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)

Please help me how to fix it.

请帮助我如何修复它。

Thanks!!!

谢谢!!!

回答by Yann Ramin

You are missing the MySQL JDBC driver .jarfile, or it is not in your classpath.

您缺少 MySQL JDBC 驱动程序.jar文件,或者它不在您的类路径中。

回答by Charlie Wu

you need to put sql driver into the class path, place the driver in the same folder as your jar file and run

您需要将sql驱动程序放入类路径中,将驱动程序放在与jar文件相同的文件夹中并运行

java -classpath mysql-connector-java-5.1.22-bin.jar -jar Test.jar

java -classpath mysql-connector-java-5.1.22-bin.jar -jar Test.jar

you can download mysql java driver from http://www.mysql.com/downloads/connector/j/

您可以从http://www.mysql.com/downloads/connector/j/下载 mysql java 驱动程序

回答by rokonoid

in case if you are using maven, add following dependency in your pom.xml

如果您使用的是 maven,请在 pom.xml 中添加以下依赖项

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.13</version>
        <classifier />
    </dependency>

回答by Kalla

add MySQL jdbc driver jar file in to your classpath.

将 MySQL jdbc 驱动程序 jar 文件添加到您的类路径中。