问题
I am dynamically loading and running JavaScript code that is stored on disk in a YAML file. I would like to know if it is possible (using intelliJ) to debug the JS code even though I am not loading it from a standalone JS file. To simplify the problem description, consider the following Java code:
NashornScriptEngineFactory nashornFactory = new NashornScriptEngineFactory();
ScriptEngine engine = nashornFactory.getScriptEngine();
engine.eval("var a = 1 + 1;\nprint(a);");
How do I set a breakpoint on line two (the "print" function call) and how do I examine the value of variable "a"? If this is not possible, what would be the best workaround?
回答1:
Based on this blog post https://blogs.oracle.com/sundararajan/remote-debugging-of-nashorn-scripts-with-netbeans-ide-using-debugger-statements, you can just attach a remote java debugger to process.
You can do this in IntelliJ IDEA by creating a new remote run configuration.
After attaching, use the JavaScript command:
debugger;
This will force the debugger to break if it is attached. You can then inspect the values of variables within the variable window.
If you can't manage to attach IntelliJ, open the browsers inspector/debugger and this same line of javascript will cause the browser's debugger to break on that line.
来源:https://stackoverflow.com/questions/38513138/debug-dynamically-loaded-javascript-code-with-intellij-and-the-nashorn-engine