Tomcat on Debian and Ubuntu Linux – Fix Broken com.mysql.jdbc.Driver Connection

By | 2014/02/20

Getting java.lang.ClassNotFoundException: “com.mysql.jdbc.Driver” or similar error on Debian? Debian and Ubuntu provide complete Apache Tomcat packages including the java mysql connector. However, the default Tomcat config does not find the provided mysql connector! Here is a fix.


1. First, this assumes you have tomcat and the mysql connector installed:

$ sudo apt-get install tomcat7 tomcat7-admin libmysql-java

The mysql connector is installed to /usr/share/java/ .


2. Next, create a symlink as below:

sudo ln -s /usr/share/java/mysql-connector-java.jar /var/lib/tomcat7/commons/mysql-connector-java.jar


3. Restart tomcat:

$ sudo /etc/init.d/tomcat7 restart

Done! Now your app should be able to connect to a mysql database.

I will submit a bug report for this on Launchpad soon.

9 thoughts on “Tomcat on Debian and Ubuntu Linux – Fix Broken com.mysql.jdbc.Driver Connection

  1. rafa

    Thank you so much!!!!!!!!!!!
    I spent many hours to find solution to this issue.

    Reply
  2. kszosze

    Theorically follow the official documentation you musn’t touch anything in common.loader and system.loader and only add your folder into the shared.loader. The common.loader and system.loader are used by tomcat to keep it’s own libraries. By the way, this tree lines tell tomcat from where must load the jar files, so added three times, you are override the library three times.

    Reply
    1. Scott Miller Post author

      Thanks! I’ve updated the post.

      Reply
  3. FengYu

    Hi author.

    Today I have the same issue, ubuntu 14.04 tomcat throw java.lang.ClassNotFoundException: “com.mysql.jdbc.Driver” . I just copy mysql jdbc from WEB-INF/lib to /usr/share/tomcat7/lib, it’s fixed.

    I don’t know why. The WEB-INF/lib contains many jar files, why just lost mysql jdbc, and others is OK ?

    I try to download tomcat.tar.gz from tomcat.apache.org , and put my app.war in it. It works , not throw any exceptions.

    It’s really surprising.

    You said you would submit a bug report. Did Launchpad reply”

    Reply
  4. feng sui

    Thanks so much for your site ! Every other site including stack* had crappy resolutions to this stupid tomcat7/ubuntu/mysql triad to work !! Cheers !

    Reply
  5. Emmanuel Bourg

    Adding /usr/share/java to the shared classpath is a very bad idea, it will pollute the classpath of the webapps and may lead to weird errors. You should instead add a symlink to the mysql driver in the /var/lib/tomcat7/common/ directory:

    ln -s /usr/share/java/mysql-connector-java.jar /var/lib/tomcat7/commons/mysql-connector-java.jar

    Reply
      1. Emmanuel Bourg

        Thanks, there was a typo though, it should be ln instead of ls.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.