如何检查@Grab是否装了罐子?脚本抛出ClassNotFoundException

时间:2013-10-31 13:13:15

标签: jdbc groovy

Groovy版本:2.0.8

我正在尝试编写一个从私有maven存储库获取Oracle JDBC驱动程序的脚本(不需要身份验证):

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")

    }
}

如何检查我的罐子是否装满了?目前我正在java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

1 个答案:

答案 0 :(得分:1)

这可能是一个类加载器问题,请尝试添加@GrabConfig(systemClassLoader=true),如下所示:

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")

    }
}