Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 463
Geoff Avatar asked Jun 14 '12 21:06

Geoff


2 Answers

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.)
like image 87
Udo Held Avatar answered Oct 28 '22 00:10

Udo Held


You must include the ojdbc6.jar file in the Deployment Assembly of the Project...

  1. select the web project which contains the jsp file...

  2. select Project tab in the menu bar in Eclipse

  3. select properties in the drop down menu

  4. select Deployment Assembly

  5. Add your ojdbc6.jar file in it.

like image 28
sri Avatar answered Oct 28 '22 01:10

sri