Sundararajan's Weblog

Remote debugging of nashorn scripts with NetBeans IDE using "debugger" statements

You can debug nashorn scripts - even if you do not create any NetBeans project or even open the JavaScript files in NetBeans! You just need to connect NetBeans debugger to a remote Java process that evaluates Nashorn scripts!

You can use ECMAScript debugger statements to stop the script execution at the places of interest.

Start the process running your script with debugger agent

I'm using "jjs" tool to run a sample script - but you may use any java application that evaluates Nashorn scripts via javax.script API.

File: test.js

print("in test script");
var obj = { foo: 2 };

jjs command with debugger turned on

jjs -J-agentlib:jdwp=server=y,suspend=y,transport=dt_socket test
Listening for transport dt_socket at address: 50349

Set breakpoint in nashorn's internal DEBUGGER method

Nashorn compiles ECMAScript "debugger" statements as calls to jdk.nashorn.internal.runtime.ScriptRuntime.DEBUGGER method.

Use Debug->New Breakpoint... menu to create a new breakpoint. Add breakpoint in jdk.nashorn.internal.runtime.ScriptRuntime.DEBUGGER method.

Attach NetBeans to remote process

Use Debug->Attach Debugger... menu to attach NetBeans to the debuggee process

Getting to 'debugger' statements

Whenever script executes ECMAScript "debugger" statement, breakpoint hits into that nashorn DEBUGGER method. On pressing F8, execution control reaches the "debugger" statement in script file. NetBeans automatically opens the script file and stops at the "debugger" statement!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.