1

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.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
mauvilsa
  • 150
  • 1
  • 12
  • I'm wondering how this would work at all, because your link goes to the trunk, which uses a different API than 2.0.20. What you need would be here: https://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java?view=markup – Tilman Hausherr Aug 04 '20 at 07:56
  • The link is just to show what I want to run. The groovy script does not use this link, so this is not an issue. From my understanding what is important is `org.apache.pdfbox.examples.util` which works correctly from what I see. – mauvilsa Aug 04 '20 at 08:04
  • Above code WFM. So it's more likely, that the error is with the PDF files provided as arguments or something else is missing. – cfrick Aug 04 '20 at 08:38
  • @cfrick I have edited the question including a specific pdf and groovy version to make it fully reproducible. – mauvilsa Aug 04 '20 at 08:56
  • Still WFM. This is with groovy 3.0.5 and 2.4.20 and java 11 in case it matters – cfrick Aug 04 '20 at 09:00
  • 1
    What happens when args is empty. Does the usage message come up? – Tilman Hausherr Aug 04 '20 at 10:25
  • 1
    FYI - It ran fine and worked as expected using groovy 2.4.7. I tried using both RemoveAllText.main and new RemoveAllText().main - both worked. – pczeus Aug 04 '20 at 12:10
  • 2
    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. – daggett Aug 04 '20 at 12:12
  • 1
    I am pretty sure that's it - my script was named differenlty and the same name would explain an infinite loop. – cfrick Aug 04 '20 at 14:51
  • @daggett yes, renaming the script fixes the problem. Can you please add this as an answer so that I can mark it as resolved. – mauvilsa Aug 04 '20 at 16:23

1 Answers1

3

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.

daggett
  • 26,404
  • 3
  • 40
  • 56