In my app, I load a script file from the classpath into a String, then pass it into the ScriptEngine. Howerver, the breakpoint set in the script file doesn't trigger. How can I make it work? I use Intellij IDEA.
ScriptEngine engine = ...;
String script = FileUtils.readFileToString(file);
Bindings bindings = engine.createBindings();
Object result = engine.eval(script, bindings);
Since the ScriptEngine.eval()
method only takes the script as a String
or as a generic Reader
, I don't think it is possible to achieve this. The GroovyScriptEngineImpl
class will generate a script name and compile it to a class at runtime, which will make it hard (impossible?) for the debugger to know which breakpoint(s) are associated with the running script.
It might not be a solution for you, but if you instead invoke the script using GroovyShell
, then it pretty much works out of the box.
Example:
File file = new File(scriptDir, "ScriptToRun.groovy");
Binding binding = new Binding();
Object result = new GroovyShell(binding).evaluate(file);
Just remember to set the correct package in the script if it is not located at the root.