如何在Netbeans的Nashhorn(JDK 8+)中调试JavaScript? [英] How to debug JavaScript in Nashhorn (JDK 8+) in Netbeans?

查看:241
本文介绍了如何在Netbeans的Nashhorn(JDK 8+)中调试JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到最新的 Netbeans 8.0 beta 提供了一种调试执行JavaScript代码"的方法在Nashorn中"(请参阅​​附件)

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] 通过打开文件(在任何项目外部),在文件内部单击,右键单击,然后选择运行文件"选项,我能够运行简单的JavaScript文件.

[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.

我需要提及的是,快捷键"Shift-F6"对我不起作用.

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] JavaScript Nashorn 上的新文章提到了Netbeans中的JavaScript调试

[UPDATE 2] A new article on JavaScript Nashorn mention about JavaScript debug in Netbeans

[UPDATE 3] 似乎Nashhorn将在JDK中弃用.请参见 JEP 335:弃用Nashorn JavaScript引擎

[UPDATE 3] It seems that Nashhorn will be deprecated inside JDK. See JEP 335: Deprecate the Nashorn JavaScript Engine

推荐答案

您需要在JDK8上运行IDE或通过Tools | Java Platforms将JDK8添加为Platform.创建一个新的Java项目.创建之后,请确保它使用JDK8平台:

You 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:

  1. 在项目"窗口中右键单击项目
  2. 选择属性"->库"并检查"JDK平台"设置

接下来,您可以:

  • 例如,如果您在此Java项目中创建一个JavaScript文件,右键单击该文件,可以运行或调试它.在这种情况下,它将真正使用Nashorn运行/调试单个JS文件
  • ,或者您可以从Java代码启动JavaScript代码,例如:

  • 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);
}

  • ,或者您可以使用以下代码以Java代码加载JS文件:

  • 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);
    }
    

  • 使用此方法,如果像这样运行/调试Java项目,则可以像往常一样向JavaScript代码以及Java代码添加断点/监视,并且在执行JS代码时,调试器将在适当的行暂停,无论如果它们属于JS或Java.

    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.

    因此,如果您具有file.js JavaScript文件,则可以在IDE中打开它,添加行断点,并使用上面的第二个代码段加载该文件时,调试器将在行断点处停止.

    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.

    更新:可用的示例项目这里.进行检查,它包含在Nashorn中调试和运行JS的所有(对我而言)已知方法.

    UPDATE: Sample project available here. Check it out, it contains all known (to me) ways to debug and run JS in Nashorn.

    这篇关于如何在Netbeans的Nashhorn(JDK 8+)中调试JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆