Search code examples
nashorn

Nashorn Javascript debugging with CompiledScript?


It seems mandatory to use the load(filename) Nashorn extension when wanting to be able to debug both Java and Javascript in an IDE, see e.g. Debugging Nashorn JavaScript with Intellij

Unfortunately, this prohibits the use of CompiledScript, as the above returns a ScriptObjectMirror object, and at least no compile(filename) Nashorn extension is documented.

One of the CompiledScript functionalities being lost here is e.g. CompiledScript.eval(ScriptContext context). I couldn't find a way of achieving the same using ScriptObjectMirror. Also, load(filename) will immediately execute the script, which may not be wanted.


Solution

  • You can debug any Nashorn/JavaScript with IDEs using this ->

    https://blogs.oracle.com/sundararajan/entry/remote_debugging_of_nashorn_scripts

    • introduce "debugger" statement in your script
    • Use your IDE to insert breakpoint in jdk.nashorn.internal.runtime.ScriptRuntime.DEBUGGER method
    • start "Debug" in your IDE

    See also for IntelliJ: https://stackoverflow.com/a/38513981/3189360

    With the recent NetBeans builds, the second part is not needed. NetBeans will automatically break into any "debugger" statement.

    See https://twitter.com/sundararajan_a/status/773375676103602178