Debug dynamically loaded JavaScript code with IntelliJ and the Nashorn engine

风格不统一 提交于 2019-12-01 05:31:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!