Debugging Groovy scripts running in a ScriptEngine from Eclipse
Asked Answered
V

2

10

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.

Vanny answered 1/8, 2012 at 11:57 Comment(0)
F
5

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.

Furtek answered 7/5, 2014 at 7:41 Comment(4)
The debugger will pause at log.error(... in Debugger class. It shows only variables of Debugger class. How can you get variables from the script to be evaluated in this or view the script related variables etc?Oddson
@Oddson Groovy will create Java Class objects from your script. Just go up the stack. The variables will either be local variables in the stack frame of your script class or fields of that class.Furtek
Thank you very much :) Worked with groovy Do you know if this could be make to work with jython too?Oddson
Never used jython but have a look at jython.org/docs/library/debug.htmlFurtek
E
1

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.

Efficacious answered 2/8, 2012 at 16:39 Comment(1)
In my case, the package line was missing in the script. I prefer your post in this question: #4833713Korrie

© 2022 - 2024 — McMap. All rights reserved.