How to Fix java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver [Solved] (original) (raw)

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

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 :