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:
RhinoExportVar.java - Exporting Rhino Variable to Java
How to export variable from Rhino JavaScript context to a Java object?
✍: FYIcenter
If you want to export variable from Rhino JavaScript context to a Java object,
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. Run the JavaScript code to set the JavaScript variable, str:
String js = "var str = 'Hello world!'"; c.evaluateString(s, js, null, 1, null);
3. Call get() method on the Scriptable scope object to export the variable, str, to a Java object, jsObj:
Object jsObj = s.get("str", s);
4. Call Context.toString() method to cast the Java object a string:
System.out.println(Context.toString(jsObj));
Here is the entire example program, RhinoSystemOut, that maps System.out from Java as varOut in JavaScript:
// Copyright (c) 2017 FYIcenter.com import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; public class RhinoExportVar { public static void main(String[] args) throws Exception { ContextFactory f = new ContextFactory(); Context c = f.enterContext(); Scriptable s = c.initStandardObjects(); String js = "var str = 'Hello world!'"; c.evaluateString(s, js, null, 1, null); Object jsObj = s.get("str", s); System.out.println(Context.toString(jsObj)); } }
Compile and run the example program, RhinoExportVar.java:
>\fyicenter\jdk-1.8.0\bin\javac -cp \fyicenter\rhino1_7R5\js.jar RhinoExportVar.java >\fyicenter\jdk-1.8.0\bin\java -cp .;\fyicenter\rhino1_7R5\js.jar RhinoExportVar Hello world!
⇒ RhinoPoundToKilo.java - Exchange Data with Rhino Variables
⇐ RhinoSystemOut.java - Mapping Java Object to Rhino
2017-08-08, 2514🔥, 0💬
Popular Posts:
JDK 17 java.base.jmod is the JMOD file for JDK 17 Base module. JDK 17 Base module compiled class fil...
commons-net.jar is the bytecode of Apache Commons Net library, which implements the client side of m...
What Is fop.jar? I got it from the fop-2.7-bin.zip. fop.jar in fop-2.7-bin.zip is the JAR file for F...
commons-collections4-4.2 -sources.jaris the source JAR file for Apache Commons Collections 4.2, whic...
maven-model-builder-3.8. 6.jaris the JAR file for Apache Maven 3.8.6 Model Builder module. Apache Ma...