简体   繁体   English

找不到mysql.jdbc.driver类异常

[英]mysql.jdbc.driver class not found exception

I am building an app which connect to MySQL database using intelij idea. 我正在构建一个使用intelij想法连接到MySQL数据库的应用程序。 I have imported jdbc driver through modules > dependencies and it works great when I run the app through IDE. 我已经通过模块>依赖项导入了jdbc驱动程序,当我通过IDE运行应用程序时,它的运行效果很好。 But when I build an artifact using build > build artifacts > rebuild (tried clean and build also) and I ran it using cmd I get the following error: 但是,当我使用构建>构建构件>重建(也尝试清理并构建)构建构件并使用cmd运行它时,出现以下错误:

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

I have also tried cleaning and rebuilding project but it didnt help. 我也曾尝试清洁和重建项目,但没有帮助。

Any ideas what am I doing wrong? 有什么想法我做错了吗?

The JDBC driver JAR file needs to be on the classpath when you run the code. 运行代码时,JDBC驱动程序JAR文件需要位于类路径上。 For example, if you are running your application like: 例如,如果您正在运行应用程序,例如:

java -jar Application.jar

You should use: 您应该使用:

java -classpath /path/to/Driver.jar -jar Application.jar

Found a solution myself. 我自己找到了解决方案。 Intelij idea was not adding new library dependency to artifact build path. Intelij的想法不是在工件构建路径中添加新的库依赖项。 So the solution is to recreate artifact and then module dependency is added. 因此解决方案是重新创建工件,然后添加模块依赖性。 Or just add it manualy to artifact settings 或者只是手动将其添加到工件设置

Try to use maven, it resolve all dependencies for you. 尝试使用Maven,它将为您解决所有依赖关系。 In IDEA you can start maven project: File->New->Project: chose Maven 在IDEA中,您可以启动maven项目: File-> New-> Project:选择了Maven

You don't need to search & place your dependency jars in classpath manually, just add another one dependency in pom.xml and maven download it for you. 您无需手动搜索依赖项jar并将其放置在classpath中,只需在pom.xml中添加另一个依赖项并通过maven为您下载。

在此处输入图片说明

To know which dependency snippet you need you - just search it here 要了解您需要哪个依赖项片段,请在此处搜索

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM