Skip to main content

Way to use two or more ScriptEngines that are independent each other

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2013-07-24

Hello everyone.
I am trying to use some ScriptEngines that are independent each other.
They should have independent variables, independent methods and independent constants.
However, I don't know the way to use them in such situation.
The code that follows to the text shows a sample for my problem.
The code has two ScriptEngines and I expect that they are independent each other.
But unfortunately, it seems that they share variables and methods.
Would you please tell me the solution of the problem.
Thank you.

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

/** Test code for ScriptEngine scope*/
public class TestScope {
public static void main(String[] argv){
// Create First ScriptEngine
ScriptEngineManager man=new ScriptEngineManager();
ScriptEngine scriptEngine=man.getEngineByName("jruby");
ScriptContext thisContext = scriptEngine.getContext();
Bindings engineScope = thisContext.getBindings(ScriptContext.ENGINE_SCOPE);
String script=
"CONSTANT=1 \n" + //set 1 to this constant
"def get_constant \n" +
" print 'in 1st engine, constant value is ',CONSTANT,'\n' \n"+
" return CONSTANT \n"+
scriptEngine.eval(script, engineScope);
scriptEngine.eval("get_constant", engineScope); // It displays "in 1st engine, constant value is 1" and it's my expected result.
}catch(Exception e){

// I expect that second ScriptEngine is independent from the first one. So, I use new objects.
ScriptEngineManager man2=new ScriptEngineManager();
ScriptEngine scriptEngine2=man2.getEngineByName("jruby");
ScriptContext thisContext2 = scriptEngine2.getContext();
Bindings engineScope2 = thisContext2.getBindings(ScriptContext.ENGINE_SCOPE);
String script2=
"CONSTANT=2 \n" + //set 2 to this constant
"def get_constant2 \n" +
" print 'in 2nd engine, constant value is ',CONSTANT,'\n' \n"+
" return CONSTANT \n"+
scriptEngine2.eval(script2, engineScope2); // Warning "

<script>:2 warning: already initialized constant CONSTANT" is appeared. scriptEngine2.eval("get_constant2", engineScope2); // It displays "in 2nd engine, constant value is 2". Although the result itself is OK, but the constant has been unexpectedly overwritten. }catch(Exception e){ e.printStackTrace(); }

// Use the first ScriptEngine again.
scriptEngine.eval("get_constant", engineScope); // It displays "in 1st engine, constant value is 2". I expected "1" but it returns "2".
scriptEngine.eval("get_constant2", engineScope); // It feels strange that the first engine can call the method that belongs to the second one.
}catch(Exception e){

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2013-07-24

I have solved !
It need a line
System.setProperty("org.jruby.embed.localcontext.scope", "singlethread");
before acquire scripting engines.

And the modified code output result I expected !