如何从命令行以Java的形式运行Groovy脚本?

如何从命令行以Java的形式运行Groovy脚本?,java,groovy,Java,Groovy,我正在尝试使用groovyc,但有些地方不正确: >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

我正在尝试使用groovyc,但有些地方不正确:

>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

我缺少什么?

当您使用
-cp
开关指定类路径时,其默认值(当前目录)被覆盖,因此JVM无法找到您的类

将当前目录添加到classpath中,一切正常:

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

确保如果您使用的是基于unix的系统(Linux或Mac),则类路径条目分隔符需要冒号而不是分号:

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

我不确定这些代码片段是否能正常工作,因为缺少带有main方法的类。 正确的命令行是:

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

我以前试过。。。回到我向类路径添加
c:\utils\groovy-1.8-1\lib
时。在切换到groovy all之后没有尝试过它,它的工作方式很有魅力。在Linux中,它类似于:
java-cp”/path\u to/groovy-all-x.x.x.jar:“test