Ojdbc7.jar在CLASSPATH中,但是java.lang.ClassNotFoundException:oracle.jdbc.OracleDriver

时间:2017-07-24 08:10:16

标签: java oracle jdbc oci8 java.lang.class

我在这里看到了很多类似的问题,但我找不到我的情况。

我写了一个演示程序来检查JDBC。这是它的开始:

import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.OracleConnection;
import java.io.*;

class JdbcCheckup {
    public static void main(String args[])
    throws SQLException, IOException, ClassNotFoundException {
        Class.forName("oracle.jdbc.OracleDriver");
        OracleConnection conn = null;
        conn = (OracleConnection) DriverManager.getConnection("jdbc:oracle:oci8:usr/pass@ldap:host");
        // ...

然后我运行以下内容并获得ClassNotFoundException

$ /usr/java71_64/bin/javac -cp /oracle/client/o12r1/jdbc/lib/ojdbc7.jar demo.java
$ /usr/java71_64/bin/jar -cvfm demo.jar MANIFEST.MF JdbcCheckup.class
added manifest
adding: JdbcCheckup.class(in = 2345) (out = 1356)(deflated 42%)
$ /usr/java71_64/bin/java -cp /oracle/client/o12r1/jdbc/lib/ojdbc7.jar:/oracle/client/o12r1/jlib/orai18n.jar -Djava.library.path=$ORACLE_HOME/lib -jar demo.jar
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
        at java.lang.Class.forNameImpl(Native Method)
        at java.lang.Class.forName(Class.java:199)
        at JdbcCheckup.main(demo.java:27)

ojdbc7.jar我可以看到oracle.jdbc.OracleDriver类:

$ /usr/java71_64/bin/jar -tf /oracle/client/o12r1/jdbc/lib/ojdbc7.jar | grep Driver
META-INF/services/java.sql.Driver
oracle/jdbc/OracleDriver.class
oracle/jdbc/driver/OracleDriver$1.class
oracle/jdbc/driver/OracleDriver.class
oracle/jdbc/driver/OracleDriverExtension.class
oracle/jdbc/driver/T2CDriverExtension.class
oracle/jdbc/driver/T4CDriverExtension.class

那么请解释一下我做错了什么?

1 个答案:

答案 0 :(得分:0)

非常感谢!

根据评论中的建议,我在清单文件中添加了Class-Path

$ cat MANIFEST.MF
Main-Class: JdbcCheckup
Class-Path: /oracle/client/o12r1/jdbc/lib/ojdbc7.jar /oracle/client/o12r1/jlib/orai18n.jar

$ /usr/java71_64/bin/javac -cp /oracle/client/o12r1/jdbc/lib/ojdbc7.jar demo.java
$ /usr/java71_64/bin/jar -cvfm demo.jar MANIFEST.MF JdbcCheckup.class
added manifest
adding: JdbcCheckup.class(in = 2345) (out = 1356)(deflated 42%)
$ /usr/java71_64/bin/java -Djava.library.path=$ORACLE_HOME/lib -jar demo.jar