Groovy-无法从自定义JAR加载类文件

发布于 2025-02-11 21:45:55 字数 1406 浏览 1 评论 0原文

这是非常简单的测试,我在某个地方缺少一些东西,这使我发疯。

我正在尝试调用一个自定义的罐子,该罐子中包含一个Groovy脚本的单个Groovy类。

产品类:

package test.products

public class Product{

    String name = "Soap";
}

使用产品类的Groovy脚本:

try{
    test.products.Product product = new test.products.Product();
    println(product.name);
}catch(Exception e){
    println(e);
}

product.groovy文件在test \ products文件夹中:

src
|- test.groovy
|_ test
  |_products
    |_ Product.groovy
 

我将产品类型从src dir: \ src> groovyc -cp。 。\ test \ products \ product.groovy

运行我的测试,结果与预期soap,因为类文件位于test \ product sub文件夹中

\src> groovy test.groovy
Soap

创建一个jar,包括product.Class:

\src> jar -cvf0 test-product-lib-0.2.jar test\products\Product.class
added manifest
adding: test/products/Product.class(in = 2713) (out= 2713)(stored 0%)

JAR现在与测试脚本在同一文件夹中,我删除了测试文件夹,以便SCIPT可以希望阅读JAR:

src
|- test.groovy
|_ test-product-lib-0.2.jar

尝试再次运行脚本,然后获得以下错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\wip\groovy\products\src\test.groovy: 3: unable to resolve class test.products.Product
 @ line 3, column 24.
        test.products.Product product = new test.products.Product();

我缺少什么?

This as very simple test, and I'm missing something somewhere, which is driving me crazy.

I'm trying to call a custom jar containing a single groovy class from a groovy script.

the Product class:

package test.products

public class Product{

    String name = "Soap";
}

The groovy script using the Product class:

try{
    test.products.Product product = new test.products.Product();
    println(product.name);
}catch(Exception e){
    println(e);
}

the Product.groovy file is in the test\products folder:

src
|- test.groovy
|_ test
  |_products
    |_ Product.groovy
 

I compile the Product type to class from src dir:
\src> groovyc -cp . .\test\products\Product.groovy

Run my test, and the result is as expected Soap, as the class file is located in the test\product sub folder

\src> groovy test.groovy
Soap

Create a jar including the Product.class:

\src> jar -cvf0 test-product-lib-0.2.jar test\products\Product.class
added manifest
adding: test/products/Product.class(in = 2713) (out= 2713)(stored 0%)

jar is now in same folder as the test script, and I've removed the test folder so that the scipt can hopefully read the jar:

src
|- test.groovy
|_ test-product-lib-0.2.jar

Try running the script again, and then get the following error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\wip\groovy\products\src\test.groovy: 3: unable to resolve class test.products.Product
 @ line 3, column 24.
        test.products.Product product = new test.products.Product();

What am I missing?

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

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

发布评论

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

评论(1

夜吻♂芭芘 2025-02-18 21:45:55

原来,我需要将罐子放在lib \ ext文件夹中,以使其加载。我的印象是,它将首先读取当前目录:(

一旦将jar放在Ext文件夹中,所有内容都按预期工作。

Turns out I need to place my jar in the lib\ext folder for groovy to load it. I was under the impression that it would first read the current directory :(

Once I placed the jar in the ext folder everything functioned as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文