繁体   English   中英

如何从命令行运行Groovy脚本作为Java?

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

我正在尝试使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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