Ojdbc7.jar在CLASSPATH中,但java.lang.ClassNotFoundException:oracle.jdbc.OracleDriver
问题描述:
我在这里看到很多类似的问题,但我找不到我的情况。Ojdbc7.jar在CLASSPATH中,但java.lang.ClassNotFoundException:oracle.jdbc.OracleDriver
我写了一个演示程序来检查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/[email protected]: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
所以,请解释一下我做错了吗?
答
非常感谢!
按照评论中的建议,我在清单文件中添加了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
你确定你不想'oracle/jdbc/driver/OracleDriver.class'吗? – XtremeBaumer
如果您有MANIFEST.MF中定义的类路径,则不会评估“-cp”。如何在清单中查看您的类路径? – Jens
@Jens,我的MANIFEST.MF只是定义了主要类:'Main-Class:JdbcCheckup' – Vikora