Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
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 (322)
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, ∼2995🔥, 0💬
Popular Posts:
JAX-WS is an API for building web services and clients. It is the next generation Web Services API r...
JDK 17 java.desktop.jmod is the JMOD file for JDK 17 Desktop module. JDK 17 Desktop module compiled ...
What Is javax.websocket-api-1.1. jar?javax.websocket-api-1.1. jaris the JAR file for Java API for We...
commons-collections4-4.4 -sources.jaris the source JAR file for Apache Commons Collections 4.2, whic...
What Is HttpComponents httpcore-4.4.6.jar? HttpComponents httpcore-4.4.6.jar is the JAR file for Apa...