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!

About Andrey Cheptsov

Andrey Cheptsov is IntelliJ IDEA product marketing manager at JetBrains. He's passionate about productivity, programming languages and tools.
This entry was posted in New Features and tagged , , . Bookmark the permalink.

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

    Thanks

    Harry

  2. Brad says:

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

    http://devnet.jetbrains.com/message/5512395

  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?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">