本文介绍了找不到类:在Java中使用GroovyClassLoader时,org.apache.ivy.core.report.ResolveReport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的普通脚本:

I have a groovy script like so:

@Grab('com.univocity:univocity-parsers:2.0.0')
import com.univocity.parsers.csv.*;

class MyCsvParser {

}

我想通过GroovyClassLoader在我的Java应用程序中加载此类.但是@Grab以某种方式产生了一个常春藤例外:

And I want to load this class in my java application via GroovyClassLoader. But the @Grab somehow yields in an ivy exception:

SomeJavaClass {
    void someMethod() {
         String script = FileUtils.readFileToString("the groovy File");
         Class c = new GroovyClassLoader(this.getClass().getClassLoader())).parse(script);
    }
}

堆栈:

Caused by: java.lang.ClassNotFoundException: org.apache.ivy.core.report.ResolveReport
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 41 more

当我注释掉@Grab时,一切正常.如何在GroovyClassLoader中启用Grapes?

When I comment out the @Grab everything works just fine. How can I enable the Grapes in GroovyClassLoader?

推荐答案

您应该添加常春藤依赖项.默认情况下不添加它,因为它被声明为非传递性的.常春藤是管理@Grab:

You should add the ivy dependency. It's not added by default because it's declared as non-transitive. Ivy is the library which manages the dependencies loaded by @Grab:

<dependency>
  <groupId>org.apache.ivy</groupId>
  <artifactId>ivy</artifactId>
  <version>2.4.0</version>
</dependency>

这篇关于找不到类:在Java中使用GroovyClassLoader时,org.apache.ivy.core.report.ResolveReport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 16:42