繁体   English   中英

从Eclipse中调试在ScriptEngine中运行的Groovy脚本

[英]Debugging Groovy scripts running in a ScriptEngine from Eclipse

我有一个Groovy脚本,运行方式如下:

File scriptFile = ...;
ScriptEngine engine = ...;
String script = FileUtils.readFileToString(scriptFile);
Object evalResult = engine.eval(script, bindings);

不出所料,脚本文件中设置的断点不会触发。 我可以改变什么来使它工作? 该脚本需要在较大程序(没有单独的启动配置)的上下文中运行,并且需要通过ScriptEngine ,并且该文件仅在运行时已知。

我正在使用这个hack:我已经定义了一个Java类Debugger ,它看起来像这样:

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!" );
    }
}

我在Ecipse的log.error行中有一个断点。

现在,我可以将这一行放入我想要断点的脚本中:

Debugger.stopInDebugger();

当然,它不允许我轻松地逐步完成脚本,但它总比没有好。

你的脚本文件是否在类路径的源文件夹中(听起来不是这样)? 如果没有,那就这样吧。 您还可以更改首选项以确保编译器永远不会编译脚本文件(并且可选地甚至不会复制到输出文件夹)。 转到首选项 - > Groovy - >编译器并查看脚本文件夹以实现此目的。

暂无
暂无

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

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