调试从Eclipse中在ScriptEngine中运行的Groovy脚本 [英] Debugging Groovy scripts running in a ScriptEngine from Eclipse
问题描述
我有一个Groovy脚本,运行如下:
文件脚本文件= ...;
ScriptEngine engine = ...;
String script = FileUtils.readFileToString(scriptFile);
对象evalResult = engine.eval(脚本,绑定);
不出所料,脚本文件中设置的断点不会触发。我可以改变什么来使其工作?该脚本需要在较大程序(无单独启动配置)的上下文中运行,并通过 ScriptEngine
运行,该文件仅在运行时已知。
我使用这个hack:我已经定义了一个Java类 Debugger
看起来像这样:
public class Debugger {
private static final Logger log = LoggerFactory.getLogger (Debugger.class);
/ **
*将这个方法调用到Java调试器的任何地方。
*
*< p>有时候,您有代码可能最终被执行,或者你有一个地方
*你想要停止但没有代码行。
*
*< p>在这种情况下,请使用此方法。
*
*< p>不要忘记设置断点,首先:-)
* * /
public static void stopInDebugger(){
log .error(请在Debugger.stopInDebugger()中设置一个断点,然后再试一次你刚刚做的!);
}
}
我有一个断点在$ > log.error
现在,我可以把这一行放入我想要断点的脚本中:
Debugger.stopInDebugger();
认为,它不允许我轻松地通过脚本,但它比没有更好。 / p>
I have a Groovy script which is run like this:
File scriptFile = ...;
ScriptEngine engine = ...;
String script = FileUtils.readFileToString(scriptFile);
Object evalResult = engine.eval(script, bindings);
Unsurprisingly, breakpoint set in the script file doesn't trigger. What can I change to make it work? The script needs to be run in the context of the larger program (no separate launch configuration), and through a ScriptEngine
, and the file is only known at runtime.
I'm using this hack: I've defined a Java class Debugger
which looks like this:
public class Debugger {
private static final Logger log = LoggerFactory.getLogger( Debugger.class );
/**
* Invoke this method anywhere to end up in the Java debugger.
*
* <p>Sometimes, you have code that might eventually be executed or you have a place
* where you want to stop but no code line.
*
* <p>In these case, use this method.
*
* <p>Don't forget to set a breakpoint, first :-)
* */
public static void stopInDebugger() {
log.error( "Please set a breakpoint in Debugger.stopInDebugger() and try again whatever you just did!" );
}
}
I have a breakpoint in the log.error
line in Ecipse.
Now, I can put this line into the script where I want a breakpoint:
Debugger.stopInDebugger();
Granted, it doesn't allow me to easily step through the script but it's better than nothing.
这篇关于调试从Eclipse中在ScriptEngine中运行的Groovy脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!