Question: How to avoid "cannot load Java class oracle.jdbc.OracleDriver" in a JRuby Gradle project?

Question

How to avoid "cannot load Java class oracle.jdbc.OracleDriver" in a JRuby Gradle project?

Answers 0
Added at 2016-12-11 23:12
Tags
Question

When trying to include the Oracle JDBC driver (ojdbc7.jar) in my JRuby Gradle project, I always get a "cannot load Java class oracle.jdbc.OracleDriver" at runtime. Here's my basic build.gradle:

buildscript {
    repositories { jcenter() }

    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:[1.2.2,2.0)'

        classpath 'com.github.jruby-gradle:jruby-gradle-plugin:%%VERSION%%'
        classpath 'com.github.jruby-gradle:jruby-gradle-jar-plugin:1.3.3'
    }
}

apply plugin: "com.github.jruby-gradle.jar"

repositories { jcenter() }

dependencies {
    jrubyJar "rubygems:colorize:0.7.7+"
    jrubyJar 'org.slf4j:slf4j-simple:1.7.12'
}

jrubyJar {
    initScript "${projectDir}/entrypoint.rb"
}

And here's my entrypoint.rb:

require 'java'
java_import 'java.sql.DriverManager'
java_import 'oracle.jdbc.OracleDriver'

puts "Hello world"

Output of build + run steps:

frank$ ./gradlew jrubyJar
:prepareJRubyJar UP-TO-DATE
:jrubyJar UP-TO-DATE

BUILD SUCCESSFUL

Total time: 2.027 secs

frank$ java -jar build/libs/plsql-unit-tester-jruby.jar
NameError: cannot load Java class oracle.jdbc.OracleDriver
     ...

Following the advice in How to use oracle jdbc driver in gradle project, I tried adding this to my build.gradle:

dependencies {
    compile files('lib/ojdbc7.jar')
}

But this causes an error at compile time:

Could not find method compile() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

I then tried to add it as a runtime dependency as suggested in How to add OJDBC6.jar in build.gradle file?:

 dependencies {
    runtime files('lib/odjbc7.jar')
}

But this again raises a compile error:

Could not find method runtime() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

So I'm stuck - how can I correctly add odjbc7.jar (or any external jar) as a dependency in my JRuby Gradle project?

Answers to

How to avoid "cannot load Java class oracle.jdbc.OracleDriver" in a JRuby Gradle project?

Source Show
◀ Wstecz