Monday, August 2, 2021

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver - cause and solution

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver Exception comes when you try to connect the Oracle database from Java program and Oracle driver is not available in Classpath. We have already seen How to connect Oracle database from Java program and found that, In order to connect to Oracle database, we load and register driver using Class.forName("oracle.jdbc.driver.OracleDriver") and this code loads a class at runtime using Reflection which throws ClassNotFoundException if the class in question e.g. "oracle.jdbc.driver.OracleDriver" is not found.

I have already listed various reasons of ClassNotFoundExcepiton in Java, which is also applicable in this case. By the way java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver is similar to  java.lang.ClassNotFoundException: MySQL.jdbc.driver.MySQLDriver which comes if you try to connect MySQL database from Java program and the corresponding driver is not in your classpath. 

You can refer a link for getting more ideas that can be used to fix java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver error as well.


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

In order to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver, we need these classes either in the form of JAR or classes in your application classpath. 

For Oracle 10g and 11g, these are present in ojdbc6.jar or ojdbc6_g.jar, which are the same except later is compiled with javac -g option to include debug information and tracking code. If you don't have this JAR then you can download it from http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html. 

Just download this JAR file and add into your Classpath by following steps given in How to set Classpath in Java. If you are not very comfortable with setting classpath then you can also put this JAR file in JRE/ext directory C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext which is used by extension class loader to load a class file.

Though it's not advisable and you should only do this for testing as chances of leaving that ojdbc6.jar are more which may cause in future if you have a new version of driver somewhere else in Application classpath but your Java application still picking these old drivers. See How the classloader works in Java for more details on this issue.

How to fix java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java
That's all on How to fix java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java. This is like any other standard java.lang.ClassNotFoundException except that you should know on which JAR file you can find this driver i.e. ojdbc6.jar


If you are using Eclipse then you can just include this JAR in your build path by selecting a project, right-click, properties, Java Build Path, and then adding JAR on the libraries tab. You can even verify whether oracle.jdbc.driver.OracleDriver is in your Classpath or not by using Eclipse shortcut for checking classes in project classpath.



Other debugging and troubleshooting articles from Javarevisited Blog
How to fix java.lang.UnsupportedClassVersionError in Java

4 comments :

Unknown said...

still that same error is coming...

Anonymous said...

@Vishal, what did you try? Have you included ojdbc6.jar or ojdbc6_g.jar in Your application's classpath? If you are running core Java application you can also try java - cp (path to ojdbc.jar) main class. alternatively if you are getting this error in Java web application e.g. while using Servlet and JSP, make sure you put ojdbc6.jar in your application server's lib or modify your web.xml to pick it from specific location.

Explanation : oracle.jdbc.driver.OracleDriver class is implementation of Driver interface from JDBC API oracle. when you try to load this class using Class.forName("oracle.jdbc.driver.OracleDriver"), class loader search for this class in classpath and throws ClassNotFoundException if it doesn't find i.e. java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver.

In almost all cases its the missing JAR in classpath which cause this issue.

Unknown said...

Hi I just added these ojdbc6 library to classpath by clicking on add external jars.
but still now it is giving class not found exception. Is it any matter of support like oracle 11g not support ojdbc6 or anything like that ?

Swetaswa Basak said...

Still I'm getting the same error..

Post a Comment