运行groovy脚本中的main类,使用grape。

huangapple go评论70阅读模式
英文:

Run class main from groovy script using grape

问题

我想使用@Grab来运行Java类的主方法,以便自动处理要求。更具体地说,我想运行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)

@Grab,导入和主方法的执行似乎工作正常。但主方法似乎会重复调用自身,因此导致StackOverflowError错误,如下所示。

Caught: java.lang.StackOverflowError
java.lang.StackOverflowError
        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文件以及使用在Ubuntu 18.04中使用默认存储库安装的Groovy版本2.4.16时,我会获得上述错误。运行的命令将是

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

它可以正常工作,没有问题。

英文:

I would like to run the main method of a java class by using @Grab so that requirements are taken care of automatically. More specifically I would like to run the pdfbox example https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java

I wrote the following groovy script

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

The @Grab, import and execution of main seems to work. But the main seems to recall itself repeatedly thus failing with a StackOverflowError as below.

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

I am new to groovy so I am not sure what I am doing wrong. Is what I am trying to do possible? If it is possible, how would it be done?

To make the example fully reproducible I get the above error when I use the pdf found at https://github.com/mozilla/pdf.js/raw/v2.4.456/examples/learning/helloworld.pdf and using groovy version 2.4.16 installed using the default repositories in Ubuntu 18.04. The command run would be

groovy RemoveAllText.groovy helloworld.pdf helloworld_out.pdf

If I manually download the required jar files and I run

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

it works without problem.

答案1

得分: 3

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

您的Groovy脚本问题是生成与Apache类相同的类名。

英文:

Rename your script from RemoveAllText.groovy to something else and everything should be fine.

Problem that your groovy script produces the same class name as Apache class.

huangapple
  • 本文由 发表于 2020年8月4日 15:44:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63242278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定