Debugger for JDK8’s Nashorn JavaScript in IntelliJ IDEA 13.1

Last Friday we announced the availability of IntellIJ IDEA 13.1 RC2, a new release candidate for IntelliJ IDEA 13.1 bringing final support for Java 8. In other awesome news, besides coding assistance for Java 8 the new build also provides a built-in debugger for JDK8’s Nashorn. Nashorn is the new lightweight high-performance JavaScript runtime built on top of JVM.

With IntelliJ IDEA 13.1 RC2, now you can add breakpoints to your Nashorn scripts and go through them when debugging your Java application. Let’s see how it works on a simple example. Here we have an application which loads a Java interface implementation from in a Nashorn script. The interface has a “sayHello” method with a single String parameter and returning a String.

If we open the Nashorn script, we’ll see an almost regular JavaScript file. The only difference here is that this scripts accesses the Java runtime to instantiate an instance of the “demo.HelloHelper” Java class and to define an implementation for the  “demo.HelloWorld” interface. Let’s add a breakpoint here to stop when the script is running.

When we run the application in the Debug mode, it stops at our breakpoint so we can see the state of local variables and step through the script statements.

Then we stop at another breakpoint we added, but already on the Java side. Here we can see the state of the “demo.HelloWorld” instance, returned by the script.

Now if we call the Step Into action, we’ll be able to step inside the script.

Note that here we use an instance of “demo.HelloHelper” Java class to get a “prefix.” This means that if we use the Step Into action again, we’ll get back to the Java side.

Also notice, that the Frames tab shows the exact stack of the invoked methods from both Java and Narshorn code.

As you can see, with IntelliJ IDEA it’s really easy to debug your Nashorn scripts. We’ll hope this feature will come useful for you as Nashorn is a very powerful tool for scripting your Java application.

As always your feedback is very welcome in our discussion forum and your bug reports in our issue tracker. Time’s limited though, as the release is just around the corner.

Develop with Pleasure!

This entry was posted in New Features and tagged , , . Bookmark the permalink.

26 Responses to Debugger for JDK8’s Nashorn JavaScript in IntelliJ IDEA 13.1

  1. Harry says:

    Hi Andrey,

    I have upgraded my IntelliJ to 13.1. But not sure it’s the RC2 you mentioned above, could you please confirm?

    I tried to sample codes here, which can’t pass the compile since class HelloWorld can not be located in Java code.

    Please give some hints.



  2. Brad says:

    Also, just in case someone else tries this example in 13.1+, the JS debugger is only enabled in Ultimate Edition.

  3. Arvind says:

    Kind of similar to the first issue. I have the ultimate 13.1 with Build#IU-135.475 built on March 17 2014. But I can’ step into the js file as shown in the screenshot. But the sample prints out the hello-world as shared by Brad.

  4. Nirav says:

    Thanks Brad for pointing an important missing component.

  5. John Hann says:

    Will Nashorn support be added to WebStorm, too? *crosses fingers*

  6. Benjamin says:

    Nashorn debugging works great, but IDEA seems to ignore the nashorn javascript dialect. e.g. For Each statement is marked as error or Java.type is unrecognized.

    Am I missing something?

  7. Handong Wang says:

    I tried the steps and it works.
    Does the debugging for Nashorn & JS code still work if I debug a Java application remotely? A typical case is my java code is running inside some kind of container.

  8. Handong Wang says:

    the debugger stops at breakpoints in .js file, but does not stop at breakpoints in .ts files(typescript files). Does it support breakpoints in .ts file or I did something wrong?

  9. Handong Wang says:

    I found an issue with the implementation:

    I tested it on Windows, js-file-name can not be absolute path in engine.eval(“load(‘js-file-name’)”). Otherwise debugging does not work though the js code got executed.

  10. Pingback: Java 1.8 + JavaScript Engine Nashorn + Debugging javascript file - Umut's Weblog

  11. Nick says:

    The two above issues basically make the nashorn debugging support completely useless. If you have a multi module project, and who doesn’t in more than a quick test project, doesn’t work at all =(

  12. Richard Richter says:

    This feature is great, but what if I want to preload script as string and then eval this? I know exactly what file it is – is ther any way how to tell Idea so I can debug such eval?

    Is it working only with load(…) command in eval or are there any other options?

  13. David Buccola says:

    I encountered some of the same problems mentioned in the comments above. After some amount of experimentation, however, I was able to get things working satisfactorily. I wrote up some of my notes here:

  14. David Buccola says:

    Oops. Previous post didn’t include the URL. Try this:

  15. Inshua says:

    cannot view object of js, object become nashorn JO, need do more for this feuture:

    function f(){
    var a =1;
    var b = 2;
    a ++;
    b ++;
    var c = {result: a+b}; // * become JO, in netbeans it’s {result:4.0}*
    print( c);


  16. fishgel says:

    I have clone this project.
    And I want to debug the Nashorn JavaScript use the IntelliJ IDEA debugger. But it not working.

    My work envinments as follows:

    IntelliJ IDEA 2017.1.1
    Build #IU-171.4073.35, built on April 7, 2017
    Licensed to yudong ding
    Subscription is active until April 20, 2018
    For educational use only.
    JRE: 1.8.0_112-release-736-b16 x86_64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Mac OS X 10.11.6

  17. Andrey Dernov says:

    Make sure to mark res folder as a resource root: after opening the project.

  18. Alexey Subach says:

    Hi, thanks for this great feature! Are sourcemaps also supported? I couldn’t find any example on debugging JS scripts with sourcemaps for Nashorn in IntelliJ.

  19. Giridhar says:

    Is this supported in windows (Windows 7)?

    I am able to debug in mac but not in windows.

Leave a Reply

Your email address will not be published. Required fields are marked *