简体   繁体   English

如何使用Java中的GroovyScriptEngine加载字符串输入(groovy脚本)

[英]How to load a string input(groovy script) using GroovyScriptEngine from Java

I have a string input, which is a groovy script, which contains one or more methods like : 我有一个字符串输入,这是一个groovy脚本,它包含一个或多个方法,如:

def printHello()
{
    println("Inside printHello()")
}

def setData(String str)
{  
    println("Incoming data : " + str)
}

This is in a string format, which is my input. 这是一种字符串格式,这是我的输入。

From a Java program, I want to load this string by using GroovyScriptEngine. 从Java程序,我想使用GroovyScriptEngine加载此字符串。

I am able to load and invoke the above methods by using GroovyClassLoader successfully. 我可以通过成功使用GroovyClassLoader来加载和调用上述方法。

new GroovyClassLoader().parseClass(scriptStr)

Unfortunately, by using GroovyScriptEngine , I cannot able to load the string. 不幸的是,通过使用GroovyScriptEngine ,我无法加载字符串。

I stored this string input into a temporary groovy file and tried to load. 我将此字符串输入存储到临时groovy文件中并尝试加载。 It is working. 这是工作。

But my intention is to load a string input directly by using GroovyScriptEngine. 但我的意图是使用GroovyScriptEngine直接加载字符串输入。

Is there any way to load groovy script content as string input to the GroovyScriptEngine instead of using a file path? 有没有办法将groovy脚本内容作为字符串输入加载到GroovyScriptEngine而不是使用文件路径?

Please help me on this... 请帮帮我...

Note : This is for my comparison study between GroovyClassLoader and GroovyScriptEngine. 注意:这是我在GroovyClassLoader和GroovyScriptEngine之间进行的比较研究。

GroovyClassLoader can load the class directly but cannot handle it dependency. GroovyClassLoader可以直接加载类,但不能处理它的依赖。 GroovyScriptEngine can only load the script but not the class. GroovyScriptEngine只能加载脚本而不能加载类。 However, it can handles class dependency and hot reloading. 但是,它可以处理类依赖和热重新加载。

You can find the detail here. 你可以在这里找到详细信息。 http://groovy-lang.org/integrating.html http://groovy-lang.org/integrating.html

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

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