Run class main from groovy script using grape

255 Views Asked by At

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

There are 1 best solutions below

0
On BEST ANSWER

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.