11

I've found an example for running Groovy scripts on systems that do not have Groovy installed using the groovy-all jar file. I attempted the following:

java -cp src:.:lib/* -jar lib/groovy-all-2.0.1.jar src/com/example/MyScript.groovy

The trouble is my script depends on jars in the lib directory plus two other Groovy script files located in src/com/examples. When I run this, it complains about the import statements for all of them. I can run it on a system that has Groovy installed fine by using the following:

CLASSPATH="src:.:lib/*" groovy src/com/example/MyScript.groovy 

How do I run Groovy scripts this way, using the groovy-all jar, as well as give it a classpath?

1
  • Unable to find groovy-all.jar for version 3.0.0-beta-2, any idea? Commented Jul 30, 2019 at 6:35

2 Answers 2

22

You can't combine both -jar and -cp in a java command, so you need to name the main class explicitly. Looking at the manifest of the groovy-all JAR, the main class name is groovy.ui.GroovyMain, so you need

java -cp 'src:.:lib/*' groovy.ui.GroovyMain src/com/example/MyScript.groovy

(if groovy-all were not already covered by lib/* you would need to add that to the -cp as well).

2

One way is to compile the Groovy files to "*.class" files. Then include the jar from the $GROOVY_HOME/embeddable directory and put it on the classpath.

Here is a minimalist example (the first line is a simple Unix copy; use whatever works for you):

$ cp /some-dir/groovy-1.8.5/embeddable/* . 
$ groovyc Test.groovy 
$ java -cp . groovy-all-1.8.5.jar Test 

For typical distribution, you would use Ant/Maven/Gradle to build your own jar file with the compiled Groovy (i.e. class files) in it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.