0

I have CentOS on both server and client in VM. I am trying to connect Java SE 8 with Oracle 11gR2. But it gives this error. I added ojdbc6.jar by "project properties-> libraries -> Add Jar/folder->/usr/java/jdk1.8.0_20/jre/lib/ext/ojdbc6.jar and ojdbc8.jar".

Code is:

import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.*;
class OracleCon
{
    public static void main(String args[])
{
try
{
    Class.forName("oracle.jdbc.driver.OracleDriver");
   
    Connection con=DriverManager.getConnection("jdbc:oracle:thin:@serverora11gr2:1521:orcl","scott","tiger");

    Statement stmt=con.createStatement();

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

    con.close();

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

}

}

Error is:

  Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class oracle.jdbc.driver.ClassRef
at oracle.jdbc.driver.OracleDriver.<clinit>(OracleDriver.java:262)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at OracleCon.main(OracleCon.java:10)
/home/rahul/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java       
 returned: 1
BUILD FAILED (total time: 0 seconds)
5
  • Have you added Oracle JDBC driver to your class path ? Dec 15, 2018 at 6:39
  • in /etc/profile : export CLASSPATH=/opt/instantclient_11_2:/usr/java/jdk1.8.0_20/jre/lib/ext:$CLASSPATH
    – rahul
    Dec 15, 2018 at 7:18
  • Try upgrading to a more recent version of the Oracle JDBC driver. Also try upgrading to a more recent version of Java 8 (Java 8 update 20 is pretty old). Also: try loading oracle.jdbc.OracleDriver instead of oracle.jdbc.driver.OracleDriver. Dec 15, 2018 at 9:05
  • The fact you have the lib/ext folder of your JDK on the CLASSPATH is very suspect. Dec 15, 2018 at 9:06
  • i updated java to jdk-8u192-linux-x64.rpm , changed export CLASSPATH=/opt/instantclient_11_2:/usr/java/jdk1.8.0_20/jre/lib:$CLASSPATH also changed Class.forName("oracle.jdbc.OracleDriver"); but having same problem.
    – rahul
    Dec 15, 2018 at 9:56

2 Answers 2

1

Try the latest JDBC driver which you can download from here: https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html. This error should go away.

0

you can try to re-order(put oracle dependency prior/much prior to sybase) sybase and ojdbc8 jar dependency in pom.xml

<dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>12.2.0.1</version>
        </dependency>

<dependency>
            <groupId>com.sybase.jconnect</groupId>
            <artifactId>jconn4</artifactId>
            <version>7.07-27082</version>
        </dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.