ripper234 ripper234 - 1 year ago 198
Groovy Question

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?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download