1

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.

1
  • See also github.com/aperto/sourceurl-resourcefilter for a Maven Resource Filter that will add //@ sourceURL= annotations automatically to JS source files. This will result in file names being displayed in Nashorn JS stackframes in the Netbeans debugger, even when load() is not used, e.g. with CompiledScript. Commented Oct 24, 2016 at 20:25

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thx, but setting a break point wasn't my problem, as I can already put it in the Java exception that is being thrown in my case. What I am looking for is stack frames corresponding to .js source lines, and .js source being shown when selecting those stack frames. Otherwise I have no clue what variables I am looking at :)
Well, NetBeans shows source (even when "eval"ed from a String) when that breakpoint hits. And stack frames in stack traces have to use artificial name - as there is no name associated by user. Not much can be done there. But, Nashorn implement @sourceURL / #sourceURL directives. ( bugs.openjdk.java.net/browse/JDK-8032068 ). That is useful to associate a readable name for eval'ed sources.
NetBeans is the next thing I'll try, as IDEA has a bug when debugging maven dependencies. Do you per chance know whether NetBeans will show sources also on CompiledScript invocations, i.e. without using load()?
OK NetBeans debugging works perfect, also on CompiledScript (as long as // @ sourceURL =... annotation is present). Thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.