Skip to content

Instantly share code, notes, and snippets.

@winterbe
Created March 30, 2014 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winterbe/9878092 to your computer and use it in GitHub Desktop.
Save winterbe/9878092 to your computer and use it in GitHub Desktop.
Java 8 Nashorn
package com.winterbe.java8;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.FileNotFoundException;
import java.io.FileReader;
/**
* @author Benjamin Winterberg
*/
public class Nashorn1 {
public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodException {
ScriptEngine engine = new ScriptEngineManager()
.getEngineByName("nashorn");
engine.eval(new FileReader("res/script.js"));
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction("fun", "Peter Parker");
System.out.println(result);
}
}
var fun = function(name) {
print('Hi there, ' + name);
return "success";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment