Groovy 无法加载随 Grape 安装的库

发布于 2024-10-17 02:17:00 字数 1064 浏览 1 评论 0原文

有人可以解释为什么即使我已经使用 Grape 安装了依赖项,Groovy 也无法找到 JDBC 驱动程序吗?

$ grape --version
Groovy Version: 1.7.7 JVM: 1.6.0_20

$ grape install org.apache.derby derby 10.5.3.0
:: loading settings :: url = jar:file:/opt/groovy-1.7.7/lib/ivy-2.2.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: resolving dependencies :: caller#all-caller;working
        confs: [default]
        found org.apache.derby#derby;10.5.3.0 in remote-repo
downloading http://127.0.0.1:8081/artifactory/repo/org/apache/derby/derby/10.5.3.0/derby-10.5.3.0.jar ...
        [SUCCESSFUL ] org.apache.derby#derby;10.5.3.0!derby.jar (388ms)

$ grape resolve org.apache.derby derby 10.5.3.0
/home/alex/.groovy/grapes/org.apache.derby/derby/jars/derby-10.5.3.0.jar

$ groovy file_parser.groovy records.txt csv
Caught: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
        at film_parser.run(film_parser.groovy:16)

$ groovy -cp /home/alex/.groovy/grapes/org.apache.derby/derby/jars/derby-10.5.3.0.jar file_parser.groovy records.txt csv
Inserted 1 rows.

Can someone explain why Groovy isn't able to find a JDBC driver even if I've already installed the dependency using Grape?

$ grape --version
Groovy Version: 1.7.7 JVM: 1.6.0_20

$ grape install org.apache.derby derby 10.5.3.0
:: loading settings :: url = jar:file:/opt/groovy-1.7.7/lib/ivy-2.2.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: resolving dependencies :: caller#all-caller;working
        confs: [default]
        found org.apache.derby#derby;10.5.3.0 in remote-repo
downloading http://127.0.0.1:8081/artifactory/repo/org/apache/derby/derby/10.5.3.0/derby-10.5.3.0.jar ...
        [SUCCESSFUL ] org.apache.derby#derby;10.5.3.0!derby.jar (388ms)

$ grape resolve org.apache.derby derby 10.5.3.0
/home/alex/.groovy/grapes/org.apache.derby/derby/jars/derby-10.5.3.0.jar

$ groovy file_parser.groovy records.txt csv
Caught: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
        at film_parser.run(film_parser.groovy:16)

$ groovy -cp /home/alex/.groovy/grapes/org.apache.derby/derby/jars/derby-10.5.3.0.jar file_parser.groovy records.txt csv
Inserted 1 rows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2024-10-24 02:17:01

安装依赖项并不意味着它可以立即用于您的脚本,它只是将 jar 下载到本地缓存。
依赖项的使用由注释控制。
一个具有不同驱动程序的完整工作示例,应该有所帮助

@GrabConfig(systemClassLoader=true)
@Grab('org.hsqldb:hsqldb:2.0.0')
import groovy.sql.Sql
def sql = Sql.newInstance(
  "jdbc:hsqldb:mem:database", "sa", "", "org.hsqldb.jdbcDriver")
println sql.firstRow('select * from information_schema.tables')

installing a dependency does not mean it's instantly available for your scripts, it will just download the jars to the local cache.
the usage of the dependency is controlled by annotations.
an fully working example with different drivers that should help

@GrabConfig(systemClassLoader=true)
@Grab('org.hsqldb:hsqldb:2.0.0')
import groovy.sql.Sql
def sql = Sql.newInstance(
  "jdbc:hsqldb:mem:database", "sa", "", "org.hsqldb.jdbcDriver")
println sql.firstRow('select * from information_schema.tables')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文