Sunday, 26 January 2014

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

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver Exception comes when you try to connect 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 class at runtime using Reflection which throws ClassNotFoundException if class in question e.g. "oracle.jdbc.driver.OracleDriver" is not found. I have already listed down various reason 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 corresponding driver is not in your classpath. You can refer link for getting more ideas which 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

How to fix java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java
In order to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver, we need these class either in 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 same except later is compiled with javac -g option to include debug information and tracing 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 class file, though its not advisable and you should only do this for testing, as chances of leaving that ojdbc6.jar is more which may cause in future if you have a new version of driver some where else in Application classpath but your Java application still picking this old drivers. See How classloader works in Java for more details on this issue.

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 project, right click, properties, Java Build path and then adding JAR on 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 Learn About Linux Blog

How to fix java.lang.UnsupportedClassVersionError in Java

No comments:

Post a Comment