Custom JavaScript functions from Java: doesn't work with explicit context?
<h3>PreWork</h3>

<p>I am able to invoke or evaluate javascript expressions from java as follows:</p>

<pre><code>jsEngine.eval("ComputerName == 'mymachine'", myScriptContext);
</code></pre>

<p>The attribute value ComputerName is read from myScriptContext object.</p>

<h3>Scenario</h3>

<p>Now the problem is that I want to have some custom functions which can be part of the evaluated expression like:</p>

<pre><code>jsEngine.eval("startswith(ComputerName, 'my')", myScriptContext);
</code></pre>

<p>Since startswith is not an available function (I must support startswith with that signature), I tried to write a .js file, where I defined my custom functions &amp; also appended my expression to be evaluated to that script and executed as follows:</p>

<pre><code>jsEngine.eval("_js_having_myfunction_and_expression_"); // note - no myScriptContext
</code></pre>

<h3>Problem</h3>

<p><code>startswith</code> gets recognized by JS engine ONLY IF I DO NOT present myScriptContext. The moment I present myScriptContet, I get this exception:</p>

<p>Exception in thread "main":</p>

<blockquote>
  <p>javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: startswith is not a function</p>
</blockquote>
