使用IntelliJ和Nashorn引擎调试动态加载的JavaScript代码 [英] Debug dynamically loaded JavaScript code with IntelliJ and the Nashorn engine
问题描述
我正在动态加载和运行存储在YAML文件中的磁盘上的JavaScript代码。我想知道是否有可能(使用intelliJ)调试JS代码,即使我没有从独立的JS文件加载它。要简化问题描述,请考虑以下Java代码:
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);");
如何在第二行设置断点(打印函数调用)以及如何检查变量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?
推荐答案
基于此博客帖子 https://blogs.oracle.com/sundararajan / remote-debugging-of-nashorn-scripts-with-netbeans-ide-using-debugger-statements ,你可以附加一个远程java调试器来处理。
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.
您可以通过创建新的远程运行配置在IntelliJ IDEA中执行此操作。
You can do this in IntelliJ IDEA by creating a new remote run configuration.
附加后,使用JavaScript命令:
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.
如果您无法设置附加IntelliJ,请打开浏览器检查器/调试器和同一行的JavaScript将导致浏览器的调试器在该行中断。
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.
这篇关于使用IntelliJ和Nashorn引擎调试动态加载的JavaScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!