SCRIPTING / CATATAN</>↗Scripting31 Jul 2009
Here you can see how to import a Java class so that you can use the class, creates an instance of it in the scripting environment. We want to print out the current date on the console. For this we need to import the Date class that’s packaged under the java.util package. In the script […]
SCRIPTING / CATATAN</>↗Scripting28 Jul 2009
This example show how to modify Java object from scripting environment. Below we manipulate a collection of string data. To pass data into the scripting engine we use a key-value binding to the script engine. package org.kodejava.script; import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptException; import java.util.List; import java.util.ArrayList; public class M...
SCRIPTING / CATATAN</>↗Scripting28 Jul 2009
This example demonstrate how you can access Java object from a script. We start by creating the ScriptEngine object. Then we obtain the Bindings object from the engine and set the polyglot.js.allowHostAccess options to true. We do this using the Bindings‘s put(String name, Object value) method. After that we can put Java object into the […]
SCRIPTING / CATATAN</>↗Scripting28 Jul 2009
This example show you how you can obtain a script engine for a specific language name and specific language version. In the code below we try to obtain script engine instance for ECMAScript version ECMAScript 262 Edition 11. package org.kodejava.script; import javax.script.ScriptEngineManager; import javax.script.ScriptEngineFactory; import javax.script.ScriptEngine; import javax.script.ScriptException; import java.util.List; public class ScriptEngineSearch { public […]
SCRIPTING / CATATAN</>↗Scripting23 Jul 2009
This code demonstrate the use of Invocable interface to invoke a specific function of a script. The Invocable.invokeFunction() takes the function name with or without a parameter as the function’s parameter. The parameter value can be passed as a varargs. package org.kodejava.script; import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptException; import javax.script.Invocable; public class InvokingFunction { public […]
SCRIPTING / CATATAN</>↗Scripting20 Jul 2009
This example demonstrated how to evaluate script that stored on a file. As we know that the eval() method can also accept a Reader object we can then read the script file using FileReader and pass it as the parameter to the eval() method of the ScriptEngine for further evaluation. package org.kodejava.script; import javax.script.ScriptEngineManager; import […]