Created
May 8, 2014 10:50
-
-
Save cristobal/b3c34a6cc3b355c740f3 to your computer and use it in GitHub Desktop.
Java 8 Nashorn Tutorial - from http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.script.Invocable; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineManager; | |
import javax.script.ScriptException; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.time.LocalDateTime; | |
import java.util.Date; | |
import java.util.Properties; | |
public class NashornModule { | |
public final static void evalStatement() { | |
System.out.println("NashornModule :: evalStatement \n"); | |
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); | |
try { | |
engine.eval("print('Hello World!');"); | |
} | |
catch(ScriptException exception) { | |
System.err.println("Failed to evaluate exception"); | |
} | |
System.out.println("\n"); | |
} | |
public final static void evalScript() { | |
System.out.println("NashornModule :: evalScript \n"); | |
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); | |
try { | |
engine.eval(new FileReader("script.js")); | |
} | |
catch (FileNotFoundException exception) { | |
System.err.println("Failed to find script.js"); | |
System.exit(0); | |
} | |
catch(ScriptException exception) { | |
System.err.println("Failed to evaluate FileReader(script.js)"); | |
System.exit(0); | |
} | |
Invocable invocable = (Invocable) engine; | |
try { | |
Object result = invocable.invokeFunction("fun1", "Peter Parker"); | |
System.out.println(result); | |
System.out.println(result.getClass()); | |
} | |
catch(NoSuchMethodException exception) { | |
System.err.println("No such method fun1"); | |
} | |
catch(ScriptException exception) { | |
System.err.println(exception); | |
} | |
System.out.println(""); | |
try { | |
invocable.invokeFunction("fun2", new Date()); | |
invocable.invokeFunction("fun2", LocalDateTime.now()); | |
invocable.invokeFunction("fun2", new Properties()); | |
} | |
catch(NoSuchMethodException exception) { | |
System.err.println("No such method fun2"); | |
} | |
catch(ScriptException exception) { | |
System.err.println(exception); | |
} | |
System.out.println("\n"); | |
} | |
public static String fun1(String name) { | |
System.out.format("Hi there from Java: %s", name); | |
return "greetings from java"; | |
} | |
public static void fun2(Object object) { | |
System.out.println(object.getClass()); | |
} | |
public static void main(String[] args) { | |
evalStatement(); | |
evalScript(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Invoking Javascript Functions from Java | |
var fun1 = function(name) { | |
print('Hi there from Javascript, ' + name); | |
return "greetings from javascript"; | |
}; | |
var fun2 = function (object) { | |
print("JS Class Definition: " + Object.prototype.toString.call(object)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment