Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When running Groovy scripts using the groovy-all jar, how do you specify a classpath?

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?

like image 662
djsumdog Avatar asked Sep 12 '12 15:09

djsumdog


2 Answers

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).

like image 129
Ian Roberts Avatar answered Sep 22 '22 07:09

Ian Roberts


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.

like image 30
Michael Easter Avatar answered Sep 22 '22 07:09

Michael Easter