10

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?

4

2 回答 2

20

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

于 2012-09-12T22:35:23.233 回答
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.

于 2012-09-12T21:11:51.377 回答