DBA > Articles

How to Fix java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver

By:
To read more DBA articles, visit http://dba.fyicenter.com/article/

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

Full article...


Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/