Skip to Main Content
  • Questions
  • "java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver"

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, PO-WEN.

Asked: April 21, 2021 - 1:27 pm UTC

Last updated: November 21, 2022 - 11:59 am UTC

Version: RDBMS 12.2.0/ Java jdk and jre 1.8.0_281

Viewed 50K+ times! This question is

You Asked

May I ask about the "java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver".
Per Java document, the PATH and CLASSPATH shall be configured in the Windows 10 environment variables .

Below are information regarding the system/database version and what I did.

The Java version is as below.
C:\>java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)

The JAVA_HOME is
C:\Program Files\Java\jre\1.8.0_281

The CLASSPATH has
C:\Program Files\Java\jdk\1.8.0_281
C:\Program Files\Java\jdk\1.8.0_281\bin
C:\Program Files\Java\jre\1.8.0_281
C:\Program Files\Java\jre\1.8.0_281\bin
C:\Oracle_RDBS_installation\product\12.2.0\client_1\bin
C:\Oracle_RDBS_installation\product\12.2.0\dbhome_1\bin

The compilation of Java program was successful by using "javac". But the class not found error message was returned during the run time.

I have no problem accessing the Oracle database from Sql*Plus at all. I created two users, created a simple table for the sake of testing.
Everything worked fine. I have no problem with the listener and the TNSNAES.ORA.

Below is the information about database:

SQL*Plus: Release 12.2.0.1.0 Production on Wed Apr 21 09:08:05 2021

Copyright (c) 1982, 2016, Oracle. All rights reserved.

Enter user-name: c##_chiara
Enter password:
Last Successful login time: Sat Apr 17 2021 09:46:21 -04:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production


Below is the Java code:
<Begin of the Java Code>



import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;


public class Thirdj3 {
public static void main(String[] args){
System.out.println("Beginning of the Thirdj3 program.");


try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");

//step2 create the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@DESKTOP-JBRID90.home:1521:orcl","c##_chiara","chiara");

//step3 create the statement object
Statement stmt=con.createStatement();

//step4 execute query
//*ResultSet rs=stmt.executeQuery("select * from emp");
//*while(rs.next())
//*System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));

//step5 close the connection object
con.close();

}
catch(Exception e){ System.out.println(e);}



}//End of main
}//End of Thirdj3 Class


<end of the java code>

and Connor said...

Your CLASSPATH needs to be able to find the jdbc drivers, so something like

set CLASSPATH=%CLASSPATH%;c:\oracle\sqldeveloper\jdbc\lib\ojdbc8.jar


should help (you change to the path to wherever your ojdbc.jar file is)

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

problem solved

Po-Wen Lu, April 28, 2021 - 12:00 pm UTC

Thanks a lot!
It works.

However, I am wondering, all the documents I read indicated only set the "lib" in the classpath where the jar file located without pointing at the specific jar file.

Is there any reason that names the specific jar file is 'required'?

Thanks,


Connor McDonald
May 03, 2021 - 4:41 am UTC

Check the classpath spec

https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

The directory lets you search for classes (not jars), although wildchars are allowed with the correct syntax.

A reader, November 18, 2022 - 7:36 pm UTC

Where to run this?
Connor McDonald
November 21, 2022 - 11:59 am UTC

In your environment variables scripts (eg .bash_profile), or the shell script you are using to run your java programs.

Or on the Windows command line, or machine properties "Environment Variables"