Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
RhinoFunctionCall.java - Call JavaScript Function from Java
How to call an JavaScript function from my Java code? I have Rhino 1.7R5 installed.
✍: FYIcenter
If you want to call an JavaScript function from my Java code, you can follow this tutorial:
1. Create a Context object and Scriptable scope object with an instance of ContextFactory:
ContextFactory f = new ContextFactory(); Context c = f.enterContext(); Scriptable s = c.initStandardObjects();
2. Create a JavaScript code to define your function and get it evaluated:
String js = "function p2k(p) {\n"; js += " return p*0.453592;\n"; js += "}\n"; c.evaluateString(s, js, null, 1, null);
3. Export the function from JavaScript to Java like any data object:
Object jsF = ScriptableObject.getProperty(s, "p2k"); Function p2k = (Function) Context.jsToJava(jsF, Function.class);
4. Call the call() on the org.mozilla.javascript.Function object. Then convert its returning object to the proper value:
Object jsR = p2k.call(c, s, s, args); Double kilo = (Double) Context.jsToJava(jsR, Double.class);
Here is the entire example program, RhinoFunctionCall, that shows how to define a function in JavaScript and run it from Java:
// Copyright (c) 2017 FYIcenter.com import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.Function; public class RhinoFunctionCall { public static void main(String[] args) throws Exception { ContextFactory f = new ContextFactory(); Context c = f.enterContext(); Scriptable s = c.initStandardObjects(); String js = "function p2k(p) {\n"; js += " return p*0.453592;\n"; js += "}\n"; c.evaluateString(s, js, null, 1, null); Object jsF = ScriptableObject.getProperty(s, "p2k"); Function p2k = (Function) Context.jsToJava(jsF, Function.class); Object jsR = p2k.call(c, s, s, args); Double kilo = (Double) Context.jsToJava(jsR, Double.class); System.out.println("Pound to Kilo conversion:"); System.out.println(" Pound = "+args[0]); System.out.println(" Kilo = "+kilo); System.out.println("JavaScript used:"); System.out.println(js); } }
Compile and run the example program, RhinoFunctionCall.java:
>\fyicenter\jdk-1.8.0\bin\javac -cp \fyicenter\rhino1_7R5\js.jar RhinoFunctionCall.java >\fyicenter\jdk-1.8.0\bin\java -cp .;\fyicenter\rhino1_7R5\js.jar RhinoFunctionCall 6.0 Pound to Kilo conversion: Pound = 6.0 Kilo = 2.721552 JavaScript used: function p2k(p) { return p*0.453592; }
⇒ RhinoJsFile.java - Running JavaScript Code from File
⇐ RhinoPoundToKilo.java - Exchange Data with Rhino Variables
2017-08-08, 2470🔥, 0💬
Popular Posts:
What Is commons-io-2.11.jar? commons-io-2.11.jar is the JAR file for Commons IO 2.5, which is a libr...
Apache BCEL Source Code Files are inside the Apache BCEL source package file like bcel-6.5.0-src.zip...
JAX-RPC is an API for building Web services and clients that used remote procedure calls (RPC) and X...
commons-collections4-4.2 -sources.jaris the source JAR file for Apache Commons Collections 4.2, whic...
commons-net.jar is the bytecode of Apache Commons Net library, which implements the client side of m...