admin管理员组文章数量:1356808
I saw that latest Netbeans 8.0 beta offers a way to debug "JavaScript code executed in Nashorn" (see attached file)
What are the steps to see this in action?
[UPDATE] I was able to run a simple JavaScript file only by opening the file (outside any project), click inside the file, right click and then select Run File option.
I need to mention that the shortcut "Shift-F6" did not worked for me.
So if anybody else knows about a different way to run this please post your solution.
[UPDATE 2] A new article on JavaScript Nashorn mention about JavaScript debug in Netbeans
[UPDATE 3] It seems that Nashhorn will be deprecated inside JDK. See JEP 335: Deprecate the Nashorn JavaScript Engine
I saw that latest Netbeans 8.0 beta offers a way to debug "JavaScript code executed in Nashorn" (see attached file)
What are the steps to see this in action?
[UPDATE] I was able to run a simple JavaScript file only by opening the file (outside any project), click inside the file, right click and then select Run File option.
I need to mention that the shortcut "Shift-F6" did not worked for me.
So if anybody else knows about a different way to run this please post your solution.
[UPDATE 2] A new article on JavaScript Nashorn mention about JavaScript debug in Netbeans
[UPDATE 3] It seems that Nashhorn will be deprecated inside JDK. See JEP 335: Deprecate the Nashorn JavaScript Engine
Share Improve this question edited Apr 4, 2019 at 6:00 Alex asked Feb 25, 2014 at 14:27 AlexAlex 5,9049 gold badges40 silver badges59 bronze badges1 Answer
Reset to default 9You need to either run IDE on JDK8 or have JDK8 added as Platform via Tools|Java Platforms. The create a new Java project. Once it is created, make sure it uses JDK8 platform:
- right click on project in Projects window
- select Properties->Libraries and check JDK Platform setting
Next you can:
- create a JavaScript file inside this Java project, if you e.g. right click on the file, you can Run it or debug it. In this case, it would really run/debug single JS file using Nashorn
or you can start JavaScript code from Java code, e.g.:
try { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("nashorn"); engine.eval("function hi(){\nvar a = 'PROSPER'.toLowerCase(); \nmiddle(); \nprint('Live long and' + a)}\n function middle(){\n var b = 1; for(var i=0, max = 5; i<max;i++){\nb++;\n}\n print('b is '+b);}hi();"); } catch (ScriptException ex) { Logger.getLogger(SampleRunner.class.getName()).log(Level.SEVERE, null, ex); }
or you can load JS file in Java code using:
try { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("nashorn"); engine.eval("load(\"" + "src/packageName/file.js" + "\");"); } catch (Exception ex) { Logger.getLogger(SampleRunner.class.getName()).log(Level.SEVERE, null, ex); }
Using this, if you run/debug Java project as such, you can add breakpoints/watches to JavaScript code as well as to Java code as usual and when the JS code is executed, debugger will pause on proper lines, no matter if they belong to JS or Java.
So if you have the file.js
JavaScript file, you can open it in IDE, add line breakpoint and when the file is loaded using the 2nd snippet above, debugger will stop on the line breakpoint.
UPDATE: Sample project available here. Check it out, it contains all known (to me) ways to debug and run JS in Nashorn.
本文标签: How to debug JavaScript in Nashhorn (JDK 8) in NetbeansStack Overflow
版权声明:本文标题:How to debug JavaScript in Nashhorn (JDK 8+) in Netbeans? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743964807a2569688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论