Tag Archives: JDBC

A quick take on ClassNotFoundException: com.mysql.jdbc.Driver

Its a common problem that programmers often face and sometimes look for a solution. The problem is clear – JVM is not able to locate the MySQL JDBC driver. We will try to solve this step-by-step using a simple java program (below) to print server’s version information. Also, before we proceed please note that I will be trying it on my Ubuntu laptop where I already have got java (openjdk) and MySQL Connector/J (libmysql-java) installed.

Now lets try to compile & run it.

$ javac Version.java 
$ java Version 
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

As expected, jvm is not able to locate the driver jar. We can easily fix this by setting the CLASSPATH environment variable to point to the MySQL Connector/J jar file (mysql-connector-java-5.1.10.jar) on the system.

$ export CLASSPATH=/usr/share/java/mysql-connector-java-5.1.10.jar:.
$ java Version 

One last thing to note here is that we have also appended ‘.‘ (i.e. the current directory) to the CLASSPATH in order to be sure that our main ‘Version’ class can also be found.