title: How do you get the JavaScript stack trace from Nashorn?
body: <p>I have a script that works just fine in Chrome but crashes on Nashorn with:</p>
  
  <blockquote>
    <p>(Error: Namespace "com.cognitect.transit.util" already declared. in  at line number 19664 at column number 6</p>
  </blockquote>
  
  <p>That error is not very useful, as that line contains:</p>
  
  <pre><code>  throw Error('Namespace "' + name + '" already declared.');
  </code></pre>
  
  <p>I need to get a full stack trace from Nashorn, I found <a href="https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/NashornException.html#getScriptStackString-java.lang.Throwable-" rel="nofollow"><code>NashornException.getScriptStackString</code></a> but the error Nashorn is generating is of type <code>javax.script.ScriptException</code> which gives me an empty string when I call <code>NashornException.getScriptStackString</code>.</p>
  
  <p>How do I get an JavaScript stack trace from Nashorn?</p>
  
  <p>I don't want to do it from JavaScript, I want to do it the same way the browser does it, no matter what JS code you are running. A lot of my JS code is third party, it's generated, I cannot modify the thousand of functions I have to print exceptions just in case.</p>