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, ∼3241🔥, 0💬
Popular Posts:
JDK 17 java.security.jgss.jmod is the JMOD file for JDK 17 Security JGSS (Java Generic Security Serv...
Java Architecture for XML Binding (JAXB) is a Java API that allows Java developers to map Java class...
JDK 11 jdk.compiler.jmod is the JMOD file for JDK 11 Compiler tool, which can be invoked by the "jav...
JDK 17 jdk.localedata.jmod is the JMOD file for JDK 17 Localedata module. JDK 17 Locale Data module ...
Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java"....