我需要制作一个不需要编译的独立Groovy脚本,并且在没有安装Groovy的情况下运行.它运行良好,但它无法识别除主脚本之外的任何其他脚本.
我的文件夹结构如下:
libs\
groovy-all-2.4.3.jar
ivy-2.4.0.jar
src\
makeRelease.groovy
ReleaseHelper.groovy
Run Code Online (Sandbox Code Playgroud)
我从src文件夹以这种方式启动脚本:
java -cp "../libs/*" makeRelease.groovy
makeRelease看起来像这样:
public class makeRelease {
public static void main(String... args) {
new ReleaseHelper()
...
}
}
Run Code Online (Sandbox Code Playgroud)
运行时这是输出:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
src\makeRelease.groovy: 5: unable to resolve class ReleaseHelper
Run Code Online (Sandbox Code Playgroud)
如何在这样的可移植脚本中包含其他类(驻留在单独的文件中)?
我认为这比你想象的容易:
libs\
groovy-all-2.4.3.jar
src\
main.groovy
Greeter.groovy
Run Code Online (Sandbox Code Playgroud)
哪里 main.groovy
public class Main {
public static void main(args) {
println 'Main script starting...'
def greeter = new Greeter()
greeter.sayHello()
}
}
Run Code Online (Sandbox Code Playgroud)
和 Greeter.groovy
class Greeter {
def sayHello() {
println 'Hello!'
}
}
Run Code Online (Sandbox Code Playgroud)
只需在类路径中添加您在单独文件中拥有类的文件夹:
java -cp .;..\libs\groovy-all-2.4.3.jar groovy.ui.GroovyMain main.groovy
Run Code Online (Sandbox Code Playgroud)
以上产量:
Main script starting...
Hello!
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2029 次 |
最近记录: |