Search code examples
javaoracle-databasejdbcclassloader

Custom URLClassLoader does not use the custom classpath


I am trying to use multiple Oralce JDBC drivers to connect to 2 Oracle databases by followed the instruction from this Question.

I have ojdbc6.jar in the classpath of the default ClassLoader to connect to Oracle 11g.

enter image description here

To connect to another database, Oracle 8i, I wrote a code as follows

File driverJar = new File("D:/workspace/ccbs/lib/classes12.jar");
String driverClassName = "oracle.jdbc.OracleDriver";
String con = "jdbc:oracle:thin:@db1host:5555:db1";
URL[] classpath = new URL[] {driverJar.toURI().toURL()};
URLClassLoader driverLoader = new URLClassLoader(classpath, ClassLoader.getSystemClassLoader());
Class driverClass = driverLoader.loadClass(driverClassName);
System.out.println(driverClass.getProtectionDomain().getCodeSource().getLocation());

In the last line, the location of the driver class is printed. Running the program and I got

file:/D:/workspace/ccbs/lib/oravl01/oracle/11.2.0.2/jdbc/lib/ojdbc6.jar

When I remove the ojdbc6.jar from the classpath and run the program again, I got

file:/D:/workspace/ccbs/lib/classes12.jar

Please suggest why my custom URLClassLoader load the oracle.jdbc.OracleDriver from ojdbc6.jar in the default classpath instead of classes12.jar in the custom classpath.


Solution

  • The answer is the class loader delegation model. ClassLoader documentation describes it.

    To resolve your issue try to just replace

    URLClassLoader driverLoader = new URLClassLoader(classpath, ClassLoader.getSystemClassLoader());
    

    with

    URLClassLoader driverLoader = new URLClassLoader(classpath, null);
    

    This way you make sure that URLClassLoader doesn't use the application class path.