ClassNotFoundException oracle.jdbc.driver.OracleDriver only in servlet, using Eclipse

Geoff picture Geoff · Jun 14, 2012 · Viewed 97.8k times · Source

The code below fails on the line:

Class.forName("oracle.jdbc.driver.OracleDriver");

with the error:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

The two printlns print:

Wed_Jun_22_11:18:51_PDT_2005
false

This makes me think the class exists and can be found. Also this exact same class works in an a non-servlet application.

I have rebooted everything multiple times and regenerated the application/servlet multiple times. All values have been hard coded to make it simple and short.

private static Connection getDBConnection() throws Exception {
    System.out.println(oracle.jdbc.driver.OracleDriver.BUILD_DATE);
    System.out.println(Class.class.desiredAssertionStatus());
    //load the driver
    Class.forName("oracle.jdbc.driver.OracleDriver");

    return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "SYSTEM", "pass");
}

full servlet that fails:

package servletClass_3;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class OneMoreBookStore
 */
@WebServlet("/OneMoreBookStore")
public class OneMoreBookStore extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    private static Connection getDBConnection() throws Exception {

        System.out.println(oracle.jdbc.driver.OracleDriver.BUILD_DATE);
        System.out.println(Class.class.desiredAssertionStatus());

        //load the driver
        Class.forName("oracle.jdbc.driver.OracleDriver");
        return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "SYSTEM", "pass");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try
        {
            Connection con = getDBConnection();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

This application works:

package servletClass_3;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnect {

    private static Connection getDBConnection() throws Exception {
        System.out.println(oracle.jdbc.driver.OracleDriver.BUILD_DATE);
        System.out.println(Class.class.desiredAssertionStatus());

        //load the driver
        Class.forName("oracle.jdbc.driver.OracleDriver");
        return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "SYSTEM", "pass");
    }
    public static void main(String[] args) {
        try
        {
            Connection con = getDBConnection();
            System.out.println("connection worked");
            con.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

I'm using:

  • Eclipse JavaEE 1.4.2
  • Tomcat 7
  • jdk1.7
  • Oracle 11g R2
  • Windows 7 64bit

Answer

Udo Held picture Udo Held · Jun 14, 2012

Probably you aren't deploying the oracle driver with your application.

You have several options:

  • You can place the driver jars in your WEB-INF/lib folder
  • You export it with your application. -> Right Click on Project -> Build Path-> Configure Build Path... -> Order and Export -> Check the drivers.
  • Place the driver jars in a shared or library extension folder of your application server. (You should go with option one or two though.)