简体   繁体   中英

Unable to compile using additional library/jar file, javac classpath

I am trying to compile code with javac using the ojdbc6.jar from oracle.

I have my source in 'src' my .class in 'bin' and my 'jar' in includes.

my current javac command is:

javac -cp ".;./includes/ojdbc6.jar" src/*.java -d bin

this worked until I imported the driver in one of my source files. Now I get:

javac -cp ".;./includes/ojdbc6.jar" src/*.java -d bin
src/OracleSingleton.java:1: package oracle.jdbc.driver does not exist
import oracle.jdbc.driver.OracleDriver;

I am sure the problem is with my classpath, any idea how to fix this. I have manually checked to see whether the file is there and it is.

also seeing as I haven't got this far I might as well preempt the next question, do I have to point to this jar when running the program too? if so how.

Many thanks

I'm not sure I see the problem, but here's a working example that might shed some light. I got the driver here .

import java.sql.*;
import java.util.*;

class DriveTest {
    public static void main (String args [])
        throws SQLException, ClassNotFoundException {
        System.out.println("Current JDBC Drivers: "
            + System.getProperty("jdbc.drivers"));
        Enumeration e = DriverManager.getDrivers(); 
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
    }
}
$ javac -cp .:ojdbc6.jar DriveTest.java 
$ java -cp .:ojdbc6.jar DriveTest
Current JDBC Drivers: null
oracle.jdbc.OracleDriver@2666e815
$ jar tf ojdbc6.jar | grep OracleDriver
oracle/jdbc/OracleDriver.class
oracle/jdbc/driver/OracleDriver$1.class
oracle/jdbc/driver/OracleDriver.class
oracle/jdbc/driver/OracleDriverExtension.class

Addendum:

Looking closer, the DriverManager Service Provider mechanism specifies oracle.jdbc.OracleDriver . You should use that instead of oracle.jdbc.driver.OracleDriver .

$ cat META-INF/services/java.sql.Driver 
oracle.jdbc.OracleDriver

我认为您应该使用-classpath而不是cp,这应该可以解决问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM