How to Fix java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver [Solved]

Scenario : Your Java program, either standalone or Java web application is trying to connect to Oracle database using type 4 JDBC thin driver "oracle.jdbc.driver.oracledriver", JVM is not able to find this class at runtime. This JAR comes in ojdbc6.jar or ojdbc6_g.jar which is most probably not available in classpath.

Cause : When you connect to Oracle database from Java program, your program loads the implementation of Driver interface provided by the database vendor using class.forName() method, which throws ClassNotFoundException when driver class is not found in classpath. In case of Oracle the driver implementation is oracle.jdbc.driver.oracledriver and "java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver" error indicate that this class is  not available in classpath. Since this class is bundled into ojdbc6.jar, its usually the case of this JAR not present in Classpath


As I said this error may come even if you are connecting to Oracle database from web server like Tomcat. This is the stack-trace from Tomcat, when my Java app was failed to load this class :

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)

You can see that due to a call to Class.forName(), WebappClassLoader tries to load this class file, it only looks at WEB-INF/lib directory, so if this class is not present in any JAR file at that location then it will throw java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver



How to solve  java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver

Solution : If you already have this JAR then include this in your classpath, for example if you are running core Java application using main() method then just make sure that you use a custom classpath specified by -cp or -classpath parameter to java command and location of this JAR there.

If you are connecting to oracle database using Java web application (Servlet, JSP) then make sure that this JAR is present in WEB-INF/lib folder. If you are using tomcat to deploy your Java web application, You can also put this ojdbc.jar in apache-tomcat/lib directory, but remember that then it will not be loaded by WebAppClassLoader but its parent, which means it will be available to all web application and will not be unloaded when you remove your web application.

If you don't have ojdbc6.jar or ojdbc_g.jar then download them from http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html, both contains oracle.jdbc.driver.oracledriver, only difference is that ojdbc_g.jar has been compiled using Javac -g parameter to include more debug information, useful while debugging and printing stacktrace. Once you add this JAR file in your application's classpath there will be no more  java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver error in Java

java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver Solution


Where to download Oracle 11g and 10g JDBC driver JARs

Oracle driver, oracle.jdbc.driver.oracledriver is available in ojdbc6.jar and ojdbc_g.jar for Oracle 11g, but if you are connecting to Oracle 10g database and running on Java 1.4 or Java 1.5 then you should either use ojdbc14.jar, or use classes12.jar if your Java application is running on JDK 1.2 and JDK 1.3. Though I suggest to use debug JAR files e.g. ojdbc14_g.jar or classes12_g.jar because they are compiled using javac -g option and contain useful tracing information.

You can download Oracle JDBC driver Jar files from http://www.oracle.com/technetwork/apps-tech/jdbc-10201-088211.html. ojdbc6.jar contain classes for use with Java 1.6 version, so if your application is running on Java 6 and connecting to Oracle 11g database then use ojdbc6 and ojdbc6_g.jar. If your program is running on Java 5 and connecting ot Oracle 11g then use ojdbc5.jar or ojdbc5_g.jar. You can download Oracle 11g JDBC drive JAR files from http://www.oracle.com/technetwork/apps-tech/jdbc-112010-090769.html.


Summary

  • If you are connecting to Oracle 11g from Java and running on version Java 6 then include ojdbc6.jar or ojdbc6_g.jar in your application's classpath.
  • If you are connecting to Oracle 11g from Java 5 then include ojdbc5.jar or ojdbc5_g.jar in your application's classpath.
  • Difference between ojdbc6.jar and ojdbc6_g.jar is that later is debug version, created by compiling source file using javac -g command. They include more debugging information, useful while troubleshooting.
  • If you are connecting to Oracle 10g database from Java 1.4 or Java 5 then use ojdbc14.jar or ojdbc14_g.jar files.
  • If your Java program is connecting to Oracle 10g database and running on Java 1.2 or Java 1.3 then please use classes12.jar and classes12_g.jar. Again difference between then is just additional debug information.

That's all about how to solve java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver error in Java. In 99% cases this error is solved by putting right Oracle driver JAR into classpath, but if you still not able to solve this problem, then I suggest to check for any classpath issue e.g. sometime you are putting it into a directory which is not included in CLASSPATH or some other setting is overriding your expected classpath. If you are unsure about how to deal with that error, I suggest reading my tutorial How Java CLASSPATH Works? That will give you good idea about where to look for when classpath issues surfaces.

You can also check following articles if you are facing any issue while connecting to other popular database e.g. Oracle, SQL Server and MySQL from Java programs :
  • General Guide to solve java.lang.ClassNotFoundException in Java [guide]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver in Java? [solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]
  • How to solve java.lang.ClassNotFoundException:org.Springframework.Web.Context.ContextLoaderListener [solution]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java MySQL? [solution]
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory? [solution]
  • How to connect to MySQL database from Java Program [steps]

11 comments:

  1. I am getting java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver in Spring. I am using JdbcTemplate class and I believe I have ojdbc6.jar in my classpath, basically on Eclipse's build path, but I am still getting this error. Can you please suggest if anything special required when you connect to Oracle DB from Spring? I am using DataSource and no connection pool so far. Thanks

    ReplyDelete
  2. Thaks for this step by step solution. I was stuck with java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver error from last two hours, thanks for helping us out.

    ReplyDelete
  3. I'm trying to move to Java 8 and I'm getting the same error. Any suggestions for that?

    ReplyDelete
    Replies
    1. Java 8 shoudn't be the reason of this error, must be something else. check if you have Oracle's JDBC driver in your classpath or not.

      Delete
  4. Is it applicable on Android Development too?
    Cause i am getting this error , although, ojdbc6.jar is added as library in my android project.

    ReplyDelete
    Replies
    1. Yes Duke, it does apply to Android development, provided you are using Java JDBC to connect to Oracle database.

      Delete
  5. As a new comer for Java (switched from .Net) I was also trying to connect MSAccess 2007 under Java-8 using Class.forName syntax but getting classnotfound exception.
    Sie, Still wrestling, I will certainly appreciate if connection code is emailed at
    nukhanid@gmail.com Thanks in anticipation

    ReplyDelete
  6. It was a right on time solution for me. Thank you.

    ReplyDelete
  7. I think adding ojdbc6.jar and ojdbc6_g.jar is enough at the class path...

    ReplyDelete