运行 class 主要来自 groovy 脚本使用 grape

Run class main from groovy script using grape

我想 运行 通过使用 @Grab java class 的主要方法,以便自动处理需求。更具体地说,我想 运行 pdfbox 示例 https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java

我写了下面的groovy脚本

#!/usr/bin/env groovy
@Grab('org.apache.pdfbox:pdfbox-examples:2.0.20')
import org.apache.pdfbox.examples.util.RemoveAllText
RemoveAllText.main(args)

main 的@Grab、导入和执行似乎有效。但 main 似乎反复回忆自己,因此失败并出现如下 WhosebugError。

Caught: java.lang.WhosebugError
java.lang.WhosebugError
        at RemoveAllText.main(RemoveAllText.groovy)
        at RemoveAllText$main.call(Unknown Source)
        at RemoveAllText.run(RemoveAllText.groovy:5)
        at RemoveAllText.main(RemoveAllText.groovy)
        ...

我是 groovy 的新手,所以我不确定自己做错了什么。我正在尝试做的事情可能吗?如果可以,如何实现?

为了使示例完全可重现,当我使用在 https://github.com/mozilla/pdf.js/raw/v2.4.456/examples/learning/helloworld.pdf 中找到的 pdf 并使用 groovy 使用 Ubuntu 中的默认存储库安装的 2.4.16 版时,出现了上述错误18 月 04 日。命令 运行 将是

groovy RemoveAllText.groovy helloworld.pdf helloworld_out.pdf

如果我手动下载所需的 jar 文件并且 运行

java -cp pdfbox-2.0.20.jar:commons-logging-1.2.jar:pdfbox-examples-2.0.20.jar org.apache.pdfbox.examples.util.RemoveAllText helloworld.pdf helloworld_out.pdf

它没有问题。

将您的脚本从 RemoveAllText.groovy 重命名为其他名称,一切都应该没问题。

您的 groovy 脚本生成与 Apache class 相同的 class 名称的问题。