Java ScriptEngine for Javascript: using XML
By : GabiRepare
Date : March 29 2020, 07:55 AM
|
How do I use Java ScriptEngine to create a Javascript function
By : faizan ahmad
Date : March 29 2020, 07:55 AM
Does that help Why not creating a real Javascript function that you add to the script? code :
function ifnotempty(value,sep){
return value != undefined && value.length > 0 ? value + sep : '';
}
private String convertPsuedoFunctions(String mask)
{
return
"function ifnotempty(value,sep){return value!=undefined&&value.length>0?value+sep:'';}"
+ mask;
}
private static final String defaultFunctions =
com.google.common.io.Files.toString(new File("functions.js"),Charsets.US_ASCII);
private String convertPsuedoFunctions(String mask)
{
return defaultFunctions + mask;
}
|
Use a jar in JavaScript through Java ScriptEngine
By : user2041929
Date : March 29 2020, 07:55 AM
hop of those help? I am able to use the jar's classes within JavaScript this way, but you have to set the jar to the class path when you go to run it. I was after a solution similar to Jython/Python's where you're able to set the jar inside Java but I'm just going to create batch files and sh files and set it that way (seems easier to do it that way now). Here is my solution that works for me. To Compile Then Run: code :
cd C:\your\directory\folder\with\the\javascript\and\java\files
javac -d . ClassSpy.java FileSearch.java HelloWorld.java Main.java Parameters.java Run.java
java -cp ./;C:\ABCAPI\bin\abcapi.jar hammer.main.Main gui=true input=JavaScriptStatus.js
myvariable = Packages.abc.foo.pack.name;
var foo = new myvariable.ClassFromTheJarFile("arg1","arg2");
foo.doSomething();
var fooSister = new myvariable.AnotherCLassFromTheJarFile("arg1");
fooSister.doSomthingFromThisClass();
myvariable = Packages.abc.foo.pack.name.ClassFromTheJarFile;
var foo = new myvariable.ClassFromTheJarFile("arg1","arg2");
|
Is it possible to import javascript files to a java ScriptEngine
By : Utkarsh
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Actually I found the answer myself: as mentioned in the comment, it is possible to call several eval with different scripts, same engine, and the engine will keep the evaluated scripts in its context. So here is my code: code :
public void executeScript(String scriptName, String[] dependencies) {
try {
FileReader script = new FileReader(scriptName);
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine jsEngine = factory.getEngineByName("nashorn");
if(dependencies != null) {
for (String dependency : dependencies) {
FileReader dependencyFile = new FileReader(dependency);
jsEngine.eval(dependencyFile);
}
}
jsEngine.eval(script);
}
}
|
ScriptEngine - Calling javascript closure from java
By : Aliza Catag
Date : March 29 2020, 07:55 AM
help you fix your problem I am trying to invoke a javascript closure from Java using ScriptEngine. See below the code snippet. I removed the script engine eval code for brevity.I was able to invoke the function which has a closure but not the closure, any help is appreciated , Solved the issue, here is the fully working code code :
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
public static class TestObj {
public TestObj() {
}
public void setVariable(String name, Object value) {
System.out.println(name + ":" + value);
if (value instanceof ScriptObjectMirror) {
ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) value;
String[] ownKeys = scriptObjectMirror.getOwnKeys(true);
for (String k: ownKeys) {
System.out.println(scriptObjectMirror.get(k));
}
}
}
}
public static void main(String[] args) throws ScriptException {
System.out.println("Hello World!");
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByName("javascript");
String js = "var transform = {\n" +
"execute : function(execution) {\n" +
" print(\"hello\");" +
"execution.setVariable(\"test\",\"testing\");\n" +
" function transform(execution) {\n" +
" execution.setVariable(\"result\", {result:\"myjson object\"});\n" +
" print(\"Testing\");\n" +
" }\n" +
" return transform;\n" +
"}}";
System.out.println(engine);
engine.eval(js);
//engine.put("execution", new TestObj());
ScriptObjectMirror transform = (ScriptObjectMirror) engine.get("transform");
ScriptObjectMirror execute = (ScriptObjectMirror) transform.callMember("execute", new TestObj());
execute.call(execute,new TestObj());
System.out.println("fully working code");
}
}
Hello World!
jdk.nashorn.api.scripting.NashornScriptEngine@6d7b4f4c
hello
test:testing
result:[object Object]
myjson object
Testing
fully working code
|