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

Posted in :

這個問題很奇怪, 因為我可以 import com.mysql.cj.jdbc.Driver, 在 eclipse 裡顯示正確,但在 Run 的時候, 都會出現 class not found.

網路上很多的解法, 也試了:
https://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-eclipse

It seems the mysql connectivity library is not included in the project. Solve the problem following one of the proposed solutions:

  • MAVEN PROJECTS SOLUTION

Add the mysql-connector dependency to the pom.xml project file:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>

Here you are all the versions: https://mvnrepository.com/artifact/mysql/mysql-connector-java

  • ALL PROJECTS SOLUTION

Add the jar library manually to the project.

Right Click the project — > build path — > configure build path

In Libraries Tab press Add External Jar and Select your jar.

You can find zip for mysql-connector here

  • Explanation:

When building the project, java throws you an exception because a file (the com.mysql.jdbc.Driver class) from the mysql connectivity library is not found. The solution is adding the library to the project, and java will find the com.mysql.jdbc.Driver


都沒有效果, 最後解法是把我的 .jar 檔放到 tomcat 的 ./lib/ 目錄下, 再從 eclipse 裡去增加 lib. 也可能是因為 ./lib/ 目錄沒有設寫入, 所以執行 eclipse 的 user 的帳號權限無法寫入.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *