I am embedding some javascript in a Java application using Rhino. I am following the example on the Rhino website, executing a script by calling the Context's evaluateString method and passing the actual script in as a String.
I have a whole bunch of existing javascript code that I would like to make use of. I don't want to concatenate it all into an enormous String and pass it in to evaluateString. I would rather be able to load the code in so that I can call it from the code that I do pass into evaluateString (kind of like the AddCode method works in Microsoft's scripting control). I would like to add code like I can currently add variables by using the ScriptableObject.putProperty method.
Is there a way to do this? Can someone provide a code snippet or a link to the documentation. Thanks!
StringWriterand use itstoStringmethod. Now useevaluateStringto parse the entire javascript code and return aScriptableobject. After that you may simply use theget(String namespace,Scriptable jsObject)method of Scriptables to access any object in scope.Function.call(Scriptable context,Scriptable scope,Scriptable thisObject,Object[] args). Therefore, I don't really see the benefit of splitting your code into fragments.