问题描述:

Groovy version: 2.0.8

I'm trying to write a script that get the Oracle JDBC driver from a private maven repository (no authentication required):

package main.groovy

import groovy.sql.Sql

import java.sql.Connection

import java.sql.DriverManager

@GrabResolver(name='oracle', root='http://my-local-server:8082/maven/repo/')

@Grapes([

@Grab('com.oracle:oracle:11.1.0.7.0'),

@Grab('com.oracle:i18n:10.2.0.5')

])

class FrameworkInspector {

static void main(String[] args) {

//NullPointerException

//this.class.classLoader.getURLs().each{

// ClassLoader.systemClassLoader.addURL(it);

//}

Class.forName("oracle.jdbc.driver.OracleDriver")

java.sql.Connection connection = DriverManager.getConnection(

"jdbc:oracle:thin:@ip:port:sid", "user", "pwd");

Sql sql = new Sql(connection)

println sql.firstRow("select * from dual")

}

}

How can I check if my jar's are loaded? Currently I'm getting java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver.

网友答案:

It's probably a classloader issue, try adding @GrabConfig(systemClassLoader=true) like so:

package main.groovy

@GrabResolver(name='oracle', root='http://my-local-server:8082/maven/repo/')
@GrabConfig(systemClassLoader=true)
@Grab('com.oracle:oracle:11.1.0.7.0')
@Grab('com.oracle:i18n:10.2.0.5')

import groovy.sql.Sql

import java.sql.Connection
import java.sql.DriverManager

class FrameworkInspector {
    static void main(String[] args) {

        //NullPointerException
        //this.class.classLoader.getURLs().each{
        //    ClassLoader.systemClassLoader.addURL(it);
        //}

        Class.forName("oracle.jdbc.driver.OracleDriver")

        java.sql.Connection connection = DriverManager.getConnection(
        "jdbc:oracle:thin:@ip:port:sid", "user", "pwd");

        Sql sql = new Sql(connection)
        println sql.firstRow("select * from dual")

    }
}
相关阅读:
Top