简体   繁体   English

使用Groovy代码执行Java类

[英]Execute Java class using groovy code

I am trying to execute a java .class file using groovy script : 我正在尝试使用groovy脚本执行java .class文件:

    def sout = new StringBuffer(), serr = new StringBuffer()
    def proc = "java ${fileName}".execute()
    proc.consumeProcessOutput(sout, serr)
    proc.waitFor()
    println "out> $sout err> $serr"

It gives error: err> Error: Could not find or load main class cle47d1d78d99a44a8ba01f0bc7612ad16 (class name generated using uuid). 它给出错误: err> Error: Could not find or load main class cle47d1d78d99a44a8ba01f0bc7612ad16 (使用uuid生成的类名)。 But when I execute it in bash it gives proper output. 但是当我在bash中执行它时,它会给出正确的输出。 I checked filename, pwd and ensured that the .class file exist in the folder where groovy tries to execute command. 我检查了文件名,pwd并确保.class文件存在于groovy尝试执行命令的文件夹中。

I am able to run other commands and also compile .java file successfully using: 我能够运行其他命令,也可以使用以下命令成功编译.java文件:

def proc = "javac ${file.path}".execute()

Please suggest. 请提出建议。

What is the value of ${fileName} . $ {fileName}的值是多少? I think it contains value like "ABC.java" . 我认为它包含像“ ABC.java”这样的值。 Where as you must use command "java ${className}". 您必须在哪里使用命令“ java $ {className}”。

Use:

java ${className}

ex:
If com.abc.ABC is the class containing the main function
java com.abc.ABC 

Package is also necessary 包装也是必要的

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

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