java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver when using Sql.newInstance(......., "oracle.jdbc.OracleDriver")


(dtserekhman) #1

I’m using gradle-1.0-milestone-6 and cannot find a way to put ojdbc6.jar onto the CLASSPATH for my groovy code that calls Sql.newInstance(…, “oracle.jdbc.OracleDriver”) and which is executed from my gradle script. This causes java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver

I tried various approaches via dependencies specification mentioned in other posts, and still cannot get it to work.

When I was using gradle-1.0-milestone-3, I resolved this problem by dropping ojdbc6.jar file into the lib directory of gradle-1.0-milestone-3, but this no longer works with gradle-1.0-milestone-6.

Based on previous posts, it appears to be a common problem, but what is a real solution for it?

Thanks.


TestKit and Classpath
(Matias Bjarland) #2

Here is an example of how you can do this for HsqlDB. I didn’t have an oracle instance hanging around so couldn’t test against oracle but the procedure should be the same.

As a side note, I did try a number of methods more in the spirit of gradle: adding the class to the buildscript classpath, adding the class to the settings object classloader, using groovy grapes to do this, using the groovy root loader, etc etc. None of it worked, so if some gradle guru has a more proper way of doing this I would be interested to know. If none exists, perhaps it would be worth adding something like the groovy @GrabConfig(systemClassLoader=true) syntax to gradle?

This isn’t exactly pretty, but the best I could come up with and it works:

repositories {
  mavenCentral()
}
  configurations {
   hsql
}
  dependencies {
  hsql 'org.hsqldb:hsqldb:2.2.6'
}
  task connect << {
   def driverClass = "org.hsqldb.jdbcDriver"
  String jdbcUrl = 'jdbc:hsqldb:hsql://localhost/myalias;mem:test'
    def gcl = new GroovyClassLoader(this.class.classLoader)
  configurations.hsql.each { file ->
    println "Adding file to classpath: $file"
    gcl.addURL(file.toURI().toURL())
    }
    java.sql.Driver driver = gcl.loadClass(driverClass).newInstance() as java.sql.Driver
      def conn = driver.connect(jdbcUrl, [user: 'sa', password: ''] as Properties)
  def sql = new groovy.sql.Sql(conn)
      sql.eachRow("select * from test") { row ->
    println " >
 row --> $row"
  }
}

Hope that helps. Let me know if it solves your problem.


(dtserekhman) #3

The “prettier” solution worked! Thank you Matias!


(Matias Bjarland) #4

Mind hitting the “good answer” button so we get this thread marked as “answered” in the list of issues?


(toadie D) #5

I can’t seem to get this to work . Using the same script above, i am getting this error with gradle 1.5.x

Could not find org.hsqldb:hsqldb:2.2.6.

from this line

configurations.hsql.each { file ->


(Matias Bjarland) #6

A somewhat prettier version. Turns out we can use the gradle (as in the class Gradle) class loader for this:

repositories {
  mavenCentral()
}
  configurations {
   hsql
}
  dependencies {
  hsql 'org.hsqldb:hsqldb:2.2.6'
}
  task connect << {
     def driverClass = "org.hsqldb.jdbcDriver"
  String jdbcUrl = 'jdbc:hsqldb:hsql://localhost/myalias;mem:test'
    configurations.hsql.each { file ->
    println "Adding URL: $file"
    gradle.class.classLoader.addURL(file.toURI().toURL())
  }
      def sql = groovy.sql.Sql.newInstance(url: jdbcUrl, user: 'sa', password: '', driver: driverClass)
  sql.eachRow("select * from test") { row ->
    println " >
 row --> $row"
     }
}