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]

31 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
  8. Thank you so much son you have saved my sleep today,

    adding the jar to web-inf/lib solved my issue

    ReplyDelete
  9. Exception in thread "main" java.lang.findClassnotFoundException: Oracle.jdbc.OracleDriver how to solve it. plz help me.

    ReplyDelete
  10. Thank you. Adding jar file to tomcat/lib actually solved my issue for ClassNotFoundException

    ReplyDelete
  11. Hi I Have an errorin my java code oracle.jdbc.driver.OracleDriverjava.lang.ClassNotFoundException
    but i already configured OJDBC14

    ReplyDelete
  12. Exception class Not found PLZ help Me
    oracle.jdbc.driver.OracleDriverjava.lang.ClassNotFoundException

    package myPackage;
    import java.sql.*;


    public class dbConnection {
    public boolean validate(LoginBean loginBean) throws ClassNotFoundException
    {
    boolean status=false;

    try {

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","admin");

    // Step 2:Create a statement using connection object
    PreparedStatement ps = connection.prepareStatement("select * from adminlog where username=? and password=?");


    ps.setString(1,loginBean.getUsername());
    ps.setString(2,loginBean.getPassword());


    ResultSet rs = ps.executeQuery();
    status = rs.next();


    }
    catch (Exception e) {
    System.out.print(e);
    }
    return status;

    }
    }

    ReplyDelete
  13. main 07/13/2020 02:04:05.0683 INFO : Successfully created db connection pool to CC database: cc@jdbc:oracle:thin:@LUTL-CS19-DBA.ams.com:1521:SWGDB19
    main 07/13/2020 02:04:05.0684 DEBUG: Setup XML bypass for CC
    main 07/13/2020 02:04:05.0762 DEBUG: Oracle cache manager - DB connections in pool: 0 available; 1 active
    main 07/13/2020 02:04:07.0134 DEBUG: Connected to db: rtmobile@jdbc:oracle:thin:@LUTL-CS19-DBA.ams.com:1521:SWGDB19
    main 07/13/2020 02:04:07.0135 DEBUG: Using the RTARM Mobile Oracle connection: rtmobile@jdbc:oracle:thin:@LUTL-CS19-DBA.ams.com:1521:SWGDB19
    main 07/13/2020 02:04:07.0137 ERROR: Database driver not found.
    java.lang.ClassNotFoundException: ianywhere.ml.jdbcodbc.IDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.logica.fieldit.refdata.RefDataBaselineHelper.getConnection(RefDataBaselineHelper.java:260)
    at com.logica.fieldit.refdata.RefDataBaselineHelper.getSAConnection(RefDataBaselineHelper.java:229)
    at com.logica.fieldit.refdata.RefDataBaselineServiceUtility.setupMobileProcessorsAPI(RefDataBaselineServiceUtility.java:638)
    at com.logica.fieldit.refdata.RefDataBaselineServiceUtility.(RefDataBaselineServiceUtility.java:444)
    at com.logica.fieldit.refdata.RefDataBaselineServiceUtility.main(RefDataBaselineServiceUtility.java:1537)



    Iam using Oracle 19 and getting this error(ERROR: Database driver not found.). Can anyone please help me.

    ReplyDelete
    Replies
    1. do you have Oracle JDBC Driver JAR in your classpath?

      Delete
  14. am still getting same thing "ava.lang.classnotfoundexception oracle.jdbc.driver.oracledriver"
    and am using Java 14.0.1 and Oracle 11g XE.

    ReplyDelete
  15. am still getting same thing "ava.lang.classnotfoundexception oracle.jdbc.driver.oracledriver"
    and am using Java 14.0.1 and Oracle 11g XE.

    ReplyDelete
    Replies
    1. bro try pasting ojdbc6.jar in tomcat/lib folder

      Delete
  16. Is 11g and 19c of Oracle Database is same? can any one clarifies?

    ReplyDelete
  17. i am getting this error while using 18c | though classpath have required jar files

    ReplyDelete
  18. I was in trouble for about a week. Even stack Over Flow didn't work for me. I am damn thankful to you Sir.

    ReplyDelete
  19. I am using old application with hibernate 3 configuration which works fine with oracle 11g but not with oracle 18c. Can anybody help me on this?

    ReplyDelete
  20. I am able to compile however getting this error at run time. pfb error :

    javac -cp ojdbc6.jar Main.java

    java -cp ojdbc6.jar Main

    Error: Could not find or load main class Main
    Caused by: java.lang.ClassNotFoundException: Main

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.