0

I use maven to download the ojdbc.jar dependency. The jar already appeared in Maven Dependencies in Eclipse which I guess it is ready for use. But when I try to import OracleDriver, Eclipse cannot help me to import the class and show error on the line.

I tried Class.forName("oracle.driver.OracleDriver") for searching the class. It also shows the exception of ClassNotFound.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.techoffice.example</groupId>
    <artifactId>OracleJdbcExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <repositories>
        <repository>
            <id>codelds</id>
            <url>https://code.lds.org/nexus/content/groups/main-repo</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
    </dependencies>
</project>

Sample Code

public static void main(String[] args){
  Class.forName("oracle.jdbc.OracleDriver");
}

Exception

Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:171)
    at com.techoffice.oracle.OracleJdbcAppl.main(OracleJdbcAppl.java:18)
RubioRic
  • 2,442
  • 4
  • 28
  • 35
Ben Cheng
  • 769
  • 10
  • 25
  • I have tried to copy the jar into a folder named lib and let eclipse reference the jar. Eclipse still cannot find the class. – Ben Cheng May 10 '18 at 07:03
  • I have tried to use ojdbc7 downloaded from oracle homepage and reference it by build path. It works. – Ben Cheng May 10 '18 at 07:43

3 Answers3

1

Due to Oracle license restrictions, the Oracle JDBC driver is not available in the public Maven repository. To use the Oracle JDBC driver with Maven, you have to download and install it manually into your Maven local repository.

Use the following command for installation:

mvn install:install-file -Dfile={{Full path to your jar}}
           -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar

Or you can use Oracle Maven Repository:

The Oracle Maven repository requires a valid user registration and the user should also accept the terms and conditions by logging into http://maven.oracle.com. This username will be used in settings.xml.

Create a settings-security.xml file that holds the master password information under the local maven repository (Example: {USER_HOME}/.m2/) that is usually under the user home directory. For example: If the username is test then the path will be

Unix : /home/test/.m2/settings.xml

Windows : C:\Users\test\.m2\settings.xml

The settings.xml file is required for downloading Oracle JDBC drivers and UCP. Encrypt the user password before using it in settings.xml. Use the following commands to encrypt the user password and update settings.xml:

mvn -encrypt-password <any_master_password> or mvn -ep <any_master_password>

Source of the information

J-Alex
  • 6,881
  • 10
  • 46
  • 64
  • But I use third party repo to download it... https://code.lds.org/nexus/content/groups/main-repo – Ben Cheng May 10 '18 at 08:25
  • @Ben Cheng Did you check your classpath variable? Sometimes IDE is not fully synchronized with build tools, and has its own settings to build project – J-Alex May 10 '18 at 08:46
  • I use eclipse and reference the jar – Ben Cheng May 10 '18 at 10:23
  • Thanks. I use Eclipse as IDE. I do not know how to check the classpath variable. Is that the build path config or the run configuration of classpath? – Ben Cheng May 11 '18 at 02:29
  • @Ben Cheng maybe this will be helpful https://stackoverflow.com/questions/7005644/classnotfoundexception-when-using-user-libraries-in-eclipse-build-path – J-Alex May 11 '18 at 08:06
0

Download the "ojdbc6.jar" file to your PC. cd into the folder where it has been downloaded. Then use this command to install the library on your maven local repository (usually your ~/.m2 directory):

mvn install:install-file -Dfile=ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -DgeneratePom=true

After this, Eclipse should be able to seamlessly find the dependency.

aaguilera
  • 1,080
  • 10
  • 27
  • It is already downloaded from maven by third-party repo. Besides, I have tried to reference it instead of using maven. Still not work. But it works for ojdbc7 download from Oracle Homepage. – Ben Cheng May 10 '18 at 07:52
-1
Include the ojdbc6.jar file in the Deployment Assembly of the Project

Right Click on project -> Properties -> Deployment Assembly -> Add your ojdbc6.jar file in it

Method2: Right Click on Project -> Build Path -> Configure Build Path -> Add External JAR -> add the ojdbc6.jar