[eclipse] How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception

There is a VERY similar question to mine but in my case I don't have any duplicate jars in my build path, so the solution does not work for me. I've searched google for a couple of hours now, but none of the solutions I've found there actually resolve my issue. I'm creating a web site with some database connectivity for a homework. I'm using a MySQL database, developing in Eclipse and running on Windows.

I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver with the following code:

import java.sql.*;
//...
public void someMethodInMyServlet(PrintWriter out)
{
    Connection connection = null;
    PreparedStatement query = null;
    try {

        out.println("Create the driver instance.<br>");
        Class.forName("com.mysql.jdbc.Driver").newInstance();

        out.println("Get the connection.<br>");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
        query = connection.prepareStatement( "SELECT * FROM customers");

        //...
    } catch (Exception e)
    {
        out.println(e.toString()+"<br>");
    }
}
//...

When I run the above code I get the following output:

Create the driver instance.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

It doesn't get past the Class.forName... line and I can't figure out why! Here is what I did:

  1. Download mysql-connector.
  2. Put it in my MySQL folder C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar.
  3. Opened the project properties in Eclipse.
  4. Add External Jar to my Build Path and I selected mysql-connector-java-5.1.12-bin.jar.

Every time I attempt to use the servlet I get the same error regardless if I have the jar in there or if I don't. Could you help me figure this out?

This question is related to eclipse jakarta-ee jdbc classnotfoundexception

The answer is


Many times I have been facing this problem, I have experienced ClassNotFoundException. if jar is not at physical location.

So make sure .jar file(mysql connector) in the physical location of WEB-INF lib folder. and make sure restarting Tomcat by using shutdown command in cmd. it should work.


What you should not do do (especially when working on a shared project)

Ok, after had the same issue and after reading some answers here and other places. it seems that putting external lib into WEB-INF/lib is not that good idea as it pollute webapp/JRE libs with server-specific libraries - for more information check this answer"

Another solution that i do NOT recommend is: to copy it into tomcat/lib folder. although this may work, it will be hard to manage dependency for a shared(git for example) project.

Good solution 1

Create vendor folder. put there all your external lib. then, map this folder as dependency to your project. in eclipse you need to

  1. add your folder to the build path
    1. Project Properties -> Java build path
    2. Libraries -> add external lib or any other solution to add your files/folder
  2. add your build path to deployment Assembly (reference)
    1. Project Properties -> Deployment Assembly
    2. Add -> Java Build Path Entries
    3. You should now see the list of libraries on your build path that you can specify for inclusion into your finished WAR.
    4. Select the ones you want and hit Finish.

Good solution 2

Use maven (or any alternative) to manage project dependency


for this error:

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

you need to:

Import java.sql.*;
Import com.mysql.jdbc.Driver;

even if its not used till app running.


Since you are running it in servlet, you need to have the jar accessible by the servlet container. You either include the connector as part of your application war or put it as part of the servlet container's extended library and datasource management stuff, if it has one. The second part is totally depend on the container that you have.


Just follow these steps:

1) Install eclipse
2) Import Apache to eclipse
3) Install mysql
4) Download mysqlconnector/J
5) Unzip the zipped file navigate through it until you get the bin file in it. Then place all files that are present in the folder containing bin to C:\Program Files\mysql\mysql server5.1/ then give the same path as the address while defining the driver in eclipse.

That's all very easy guys.


Put mysql-connector-java-5.1.38-bin.jar to the C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib folder.by doing this program with execute


If the problem still persists,

Put the- mysql-connector-java-5.0.8-bin jar in a place inside your Tomcat->lib->folder (No matter where you've installed your Tomcat). And change your environmental variable (Done by clicking Properties of Mycomputer -Advanced system settings- Environmental variables-And set a new variable name & variable values as the place where your lib file resides.Dont forget to enter a ; at the end of the path)

If still problem persists Try downloading commons-collections-2.0.jar (http://www.docjar.com/jar_detail/commons-collections-2.0.jar.html) and paste the jar in the same place where your mysql jar resides (ie) inside Tomcat-lib.

Clean your project-Stop your server- Finally try to run.


The only solution worked for me is putting the .jar file under WEB-INF/lib . Hope this will help.


The others are right about making the driver JAR available to your servlet container. My comment was meant to suggest that you verify from the command line whether the driver itself is intact.

Rather than an empty main(), try something like this, adapted from the included documentation:

public class LoadDriver {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
    }
}

On my platform, I'd do this:

$ ls mysql-connector-java-5.1.12-bin.jar 
mysql-connector-java-5.1.12-bin.jar
$ javac LoadDriver.java 
$ java -cp mysql-connector-java-5.1.12-bin.jar:. LoadDriver

On your platform, you need to use ; as the path separator, as discussed here and here.


My issue was a little different. Instead of jdbc:oracle:thin:@server:port/service i had it as server:port/service.

Missing was jdbc:oracle:thin:@ in url attribute in GlobalNamingResources.Resource. But I overlooked tomcat exception's

java.sql.SQLException: Cannot create JDBC driver of class 'oracle.jdbc.driver.OracleDriver' for connect URL 'server:port/service'

assuming your project is maven based, add it to your POM:

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

Save > Build > and test connection again. It works! Your actual mysql java connector version may vary.


Place mysql-connector-java-5.1.6-bin.jar to the \Apache Tomcat 6.0.18\lib folder. Your problem will be solved.


Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to jakarta-ee

Java 11 package javax.xml.bind does not exist javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) web.xml is missing and <failOnMissingWebXml> is set to true WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default Name [jdbc/mydb] is not bound in this Context An internal error occurred during: "Updating Maven Project". java.lang.NullPointerException How to consume a SOAP web service in Java java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

Examples related to jdbc

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' Hibernate Error executing DDL via JDBC Statement Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] MySQL JDBC Driver 5.1.33 - Time Zone Issue Spring-Boot: How do I set JDBC pool properties like maximum number of connections? Where can I download mysql jdbc jar from? Print the data in ResultSet along with column names How to set up datasource with Spring for HikariCP? java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why? java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname

Examples related to classnotfoundexception

A child container failed during start java.util.concurrent.ExecutionException org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException How do I resolve ClassNotFoundException? HTTP Status 500 - Error instantiating servlet class pkg.coreServlet oracle.jdbc.driver.OracleDriver ClassNotFoundException java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...? JUNIT Test class in Eclipse - java.lang.ClassNotFoundException java.lang.ClassNotFoundException on working app