简体   繁体   English

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

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

I have a Groovy script which is run like this: 我有一个Groovy脚本,运行方式如下:

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. 该脚本需要在较大程序(没有单独的启动配置)的上下文中运行,并且需要通过ScriptEngine ,并且该文件仅在运行时已知。

I'm using this hack: I've defined a Java class Debugger which looks like this: 我正在使用这个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!" );
    }
}

I have a breakpoint in the log.error line in Ecipse. 我在Ecipse的log.error行中有一个断点。

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. 当然,它不允许我轻松地逐步完成脚本,但它总比没有好。

Is your script file in a source folder on the classpath (sounds like it's not)? 你的脚本文件是否在类路径的源文件夹中(听起来不是这样)? If not, make it so. 如果没有,那就这样吧。 You can also change your preferences to ensure that the script file is never compiled by the compiler (and optionally not even copied to the output folder). 您还可以更改首选项以确保编译器永远不会编译脚本文件(并且可选地甚至不会复制到输出文件夹)。 Go to Preferences -> Groovy -> Compiler and look at script folders to make this happen. 转到首选项 - > Groovy - >编译器并查看脚本文件夹以实现此目的。

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

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