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:
JavaScriptHello - SJP API Hello Example
How to use the SJP (Scripting for the Java Platform) API to write a simple "Hello World!" example?
✍: FYIcenter
You can follow this tutorial to use the SJP (Scripting for the Java Platform) API to write a simple "Hello World!" example:
1. Create a JavaScript ScriptEngine object with an instance of ScriptEngineManager:
ScriptEngineManager f = new ScriptEngineManager(); ScriptEngine e = f.getEngineByName("JavaScript");
2. Call the eval() method on the ScriptEngine object to run a JavaScript code:
e.eval("print(' Hello world!')");
Here is the entire sample program, JavaScriptHello.java, that shows you how to use the SJP (Scripting for the Java Platform) API:
// Copyright (c) 2017 FYIcenter.com import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; public class JavaScriptHello { public static void main(String[] args) throws Exception { ScriptEngineManager f = new ScriptEngineManager(); ScriptEngine e = f.getEngineByName("JavaScript"); System.out.println("\nFrom JavaScript:"); e.eval("print(' Hello world!')"); } }
Compile and run it:
>\fyicenter\jdk-1.8.0\bin\javac JavaScriptHello.java >\fyicenter\jdk-1.8.0\bin\java JavaScriptHello From JavaScript: Hello world!
⇒ JavaScriptEngineInfo.java - JavaScript Engine Details
⇐ What Is SJP (Scripting for the Java Platform)
2017-08-03, 1249🔥, 0💬
Popular Posts:
JDK 11 jdk.xml.dom.jmod is the JMOD file for JDK 11 XML DOM module. JDK 11 XML DOM module compiled c...
JDK 6 tools.jar is the JAR file for JDK 6 tools. It contains Java classes to support different JDK t...
What Is poi-3.5.jar - Part 2? poi-3.5.jar is one of the JAR files for Apache POI 3.5, which provides...
How to download and install ojdbc5.jar for Oracle 11g R1? ojdbc5.jar for Oracle 11g R1 is a Java 5 J...
What Is in Xerces-J-bin.2.12.2.zip? Xerces-J-bin.2.12.2.zip file is the distribution package ZIP fil...