How Do You Troubleshoot this Error “java.lang.ClassNotFoundException: com.mysql.jdbc.Driver”?

Problem scenario
In Linux you are trying to run a Java program that connects to a MySQL database. The program compiles, but it does not run. You get this error: "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" What should you do?


1. Set the CLASSPATH variable in two steps. First set it to where the .class file is for the Java file you are running. If the .class file of the program you are running is in /home/jdoe/javaprograms run this command:

export CLASSPATH=/home/jdoe/javaprograms

2. Find where the .jar file is that you need; it will be referred to as "/path/found/file.jar" below. (This command "sudo find / -name ext | grep lib" may help you find where the .jar file should be.)

3. Now set CLASSPATH by running this command: export CLASSPATH=$CLASSPATH:/path/found/file.jar

4. Now try to run the Java program again.

Leave a comment

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