简体   繁体   中英

How do I run Groovy scripts as Java, from the command line?

I'm trying to use groovyc, but something is not right:

>echo println("Hello world") > test.groovy
>groovy test.groovy
Hello world
>groovyc test.groovy
>java -cp C:\utils\groovy-1.8.1\embeddable\groovy-all-1.8.1.jar test
Error: Could not find or load main class test

>dir test.class
...

11/10/2011  02:54 PM             7,104 test.class

What am I missing?

When you specify the classpath with -cp switch, its default value (current directory) is overwritten and so JVM can't find your class.

Add current directory to classpath, and everything works:

>java -cp C:\utils\groovy-1.8.1\embeddable\groovy-all-1.8.1.jar;. test
Hello, world

Make sure that if you are using a unix based system (Linux or Mac), then you need colon instead of semicolon for classpath entry separator:

>java -cp /path/to/groovy/embeddable/groovy-all-1.8.1.jar:. test
Hello, world

I am not sure these snippets will work, since class with main method is missed. Proper command line is:

java -cp /path/to/groovy/embeddable/groovy-all-1.8.1.jar groovy.lang.GroovyShell test.groovy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM