A question about Nashorn and Netbeans debugger

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Wed Nov 25 05:57:12 UTC 2015


Hi Dina,

I used NetBeans 8.0.2 and jdk 8 update 65 on Windows 7.  I think I 
managed to reproduce the issue you described.
Steps I used are as follows:

1) create a new NB project called "script_sample" with one Main.java in 
it as source:

import javax.script.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager m = new ScriptEngineManager();
        ScriptEngine e = m.getEngineByName("nashorn");
        e.eval("load(\"" + args[0] + "\")");
        System.out.println("press any key to continue..");
        System.in.read();
        e.eval("load(\"" + args[0] + "\")");
    }
}

2) "clean and build" the NB project

3) create a simple script test.js with the following content (outside 
the NB project)

print("in test.js script");
var x = 33;
x++;
print(x);

3) in a command line shell (I used cygwin), start the java process with 
debugger server on - but not suspended at the start:

      java -cp script_sample/dist/script_sample.jar 
-agentlib:jdwp=server=y,suspend=n,transport=dt_socket Main test.js

The last command runs code up to the point of System.in.read call in 
Main.java

4) In NetBeans, open the test script file "test.js" and set breakpoint @ 
"print" call.

5) In NetBeans, use "Debug->Attach Debugger..." menu and specify the 
socket port printed by step (3)

5) enter a value for System.in.read() in command line shell.

6) No breakpoint hit @ test.js

But, if step (3) was modified to suspend process at start for debugger 
attach:

      java -cp script_sample/dist/script_sample.jar 
-agentlib:jdwp=server=y,suspend=y,transport=dt_socket Main test.js

The breakpoint in test.js enters (twice) once for each "load("test.js")" 
call.

So, there seems to be a bug somewhere (in NetBeans?). That said, there 
seems to be a simple workaround for this issue.
Within NB, set a breakpoint in Main.java (in addition to breakpoint in 
test.js) at second eval call  (line 10).  breakpoint is hit in Main.java.
At that point, keep hitting "Step Into" (F7) menu. It eventually lands 
in test.js -- and stops at the breakpoint in script!

Hope this helps,
-Sundar

On 11/24/2015 10:00 PM, Jim Laskey (Oracle) wrote:
> Dian,
>
> We’ll dive in for you, but the debugger expert (Sunder) will be signing off soon.  He’ll pick it up tomorrow.
>
> Cheers,
>
> - Jim
>
>
>
>> On Nov 24, 2015, at 12:03 PM, Dina Farbman <dina.farbman at new-proimage.com> wrote:
>>
>> Hi nashorn-dev,
>> I'm relatively new to Nashorn.
>> I work on a java 8 application that needs to run server side javascript scripts using Nashorn. The java code runs the scripts at certain points using nashorn's eval function, like this:
>>
>> SimpleBindings simpleBinding= new SimpleBindings();
>>               simpleBinding.put(....);
>> ...
>> ScriptEngineManager factory = new ScriptEngineManager();
>>             engine = factory.getEngineByName("nashorn");
>> engine.eval("load('"+scriptpath+"');", simpleBinding);
>>
>> In addition, I need to provide a way to debug those scripts.
>> The only option I could find was using Netbeans debugger (remote attach).
>> The scripts work fine.
>> The debugger, though, fails to stop at the breakpoints inside the scripts in the following scenario:
>> 1-        Server started
>> 2-        The script is running once
>> 3-        Open Netbeans, attach remote debugger
>> 4-        Run script again
>>
>> It will stop at the breakpoints inside the scripts in the following scenario:
>> 1-        Server started
>> 2-        Open Netbeans, attach remote debugger
>> 3-        Run the script
>>
>> So, what am I doing wrong?
>> What can cause such behavior?
>> Is there a reasonable workaround?
>>
>> Thanks in advance,
>> Dina



More information about the nashorn-dev mailing list