找不到mysql.jdbc.driver类的异常

时间:2015-07-31 19:16:13

标签: java mysql intellij-idea classpath

我正在构建一个使用intelij idea连接到MySQL数据库的应用程序。我通过模块导入了jdbc驱动程序>依赖关系,当我通过IDE运行应用程序时它很有用。但是当我使用build>构建工件时构建工件> rebuild(尝试清理并构建)并使用cmd运行它我得到以下错误:

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

我也尝试过清洁和重建项目,但它没有帮助。

任何想法我做错了什么?

3 个答案:

答案 0 :(得分:0)

The JDBC driver JAR file needs to be on the classpath when you run the code. 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

答案 1 :(得分:0)

Found a solution myself. Intelij idea was not adding new library dependency to artifact build path. So the solution is to recreate artifact and then module dependency is added. Or just add it manualy to artifact settings

答案 2 :(得分:0)

Try to use maven, it resolve all dependencies for you. In IDEA you can start maven project: File->New->Project: chose 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.

enter image description here

To know which dependency snippet you need you - just search it here