Debugging Nashorn / Javascript script executions: HOW-TO

This has been a question asked many times, and so wanted to document is for others:

The question is often asked: “how can i debug a script?”, “how can i debug a script without doing redeployments, etc”

using an IDE with a debugger (Such as netbeans or IntelliJ) do the following:

  1. In your script add the following on the first line of the script (including the //):

    //@ sourceURL=/absolute/path/to/MyScript.js
    

    This tells the IDE where to find the source script to show in the debug (because scripts can be passed into the ScriptEngine as a Reader or just a string, and therefore we need a source file to debug against.

  2. write your script, and add the a line debugger; to wherever you want to execute your debug. Same way as you would do in a browser debug.

  3. Run Camunda with a JVM debugger attached (Such as commonly named “Debug Application” within Intellij) and deploy your BPMN.

When the eval picks up the debugger line it will route to the file based on the sourceURL line and you will have the regular debugging tools available (including the expression evaluator)

Note: If you misconfigure the sourceURL path, the debugger will not throw any error: It will just continue as if the debugger statement did not exist.

2 Likes

Hi @StephenOTT

Would you consider turning this into a blog post?
I think it might be really handy to keep this information in a place that people will find it easier to find like the camunda blog.