2

I have installed Oracle 11.2 and Java:

java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

In the command line, if i try to:

java oracle.jdbc.driver.OracleDriver

Java says: impossibile to load or find oracle.jdbc.driver.OracleDriver

I have copied ojdbc5.jar, ojdbc6.jar and ojdbc6_g.jar

From oraclexe\app\oracle\product\11.2.0\server\jdbc\lib to
C:\Program Files\Java\jdk1.7.0_09\lib

If i run echo %CLASSPATH% I get:

C:\Program Files\Java\jdk1.7.0_09\lib (ie where I have copied the jar files)

Any reasons why Java can't find oracle.jdbc.driver.OracleDriver ?

4 Answers4

7

You reference a folder on the classpath and expect it to load all jars in it. That is not how the classpath works, you need to reference specific jars (and normally you should NOT put third party jars inside the JDK folder).

It is also important to know that the CLASSPATH is usually ignored by java applications, except for the most basic use cases.

You can do what you try to achieve by doing:

java -cp <path-to>\ojdbc7.jar oracle.jdbc.OracleDriver

This will fail btw because OracleDriver has no public static void main(String[] args) method and therefor cannot be run like this. The normal way to use a JDBC driver is to have the driver on the application classpath, and simply specify the right driver URL. JDBC 4.0 (Java 6) or higher compliant drivers will be automatically loaded from the classpath (as specified with -cp, the Class-Path manifest entry etc).

On an unrelated note, oracle.jdbc.driver.OracleDriver is considered deprecated, use oracle.jdbc.OracleDriver instead, see Difference between Oracle jdbc driver classes?

Community
  • 1
  • 1
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • The problem is that I need OracleDrive within a JSP page... (Eclipse > Dynamic Web Project) – dynamic Jul 17 '13 at 16:57
  • 1
    That is a different question, I'd suggest to search around a bit or ask a new question. Just a hint: you usually define a `DataSource` in the application server, request it through JNDI (or dependency injection) and get a `Connection` from it; in that case the driver is placed in the classpath of the application server itself, not of the web app. If you really need it in the webapp without a `DataSource`, it needs to be in the `WEB-INF/lib` folder of the application. Also, it usually is a design/architecture smell when a database is queried directly on a JSP – Mark Rotteveel Jul 17 '13 at 19:23
  • I have opened a similar question: http://stackoverflow.com/questions/17723147/java-eclipse-package-oracle-jdbc-driver-does-not-exist but none seems able to help – dynamic Jul 18 '13 at 12:41
1

Putting a directory on the classpath doesn't put all the jar files within that directory on the classpath. It's not clear why you've copied the Oracle jar file into your Java installation directory - I'd recommend not doing that - but you should just list the location explicitly. For example, if you've copied it into the lib directory relative to your application, you could use:

java -cp lib\ojdbc7.jar;. your.class.Name

You can use * in a -cp command line argument to find all jar files, e.g.

java -cp lib\*;. your.class.Name

or you could copy it into an "extensions" directory - but I think it's clearer to be explicit.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

I also had same problem and this is what i did I extracted ojdbc5.jar and then i copied oracle folder in extracted ojdbc5.jar and then pasted in current location where i wrote jdbc program ( not mentioning the directory as it differs from programmer to programmer), then used import oracle.jdbc.*; statement in my jdbc program as oracle.jdbc has OracleDriver in it. Rest of the program is same

0

Before copying ojdbc6.jar to <jdk-home>/jre/lib/ext/, in IDEA you need to add the ojdbc6.jar file in "Structure" -> "SDK" -> "add classpath" to <jdk-home>/jre/lib/ext/ojdbc6.jar

Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
Mihail
  • 1