'Run class main from groovy script using grape

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 daggett