Embedding a JavaScript snippet in a Java class method -
i have java programme opens, reads, , write multiple files. contains complex logic formatting.
now wrote on jsfiddle easy javascript here tree traversals , parsing me, , much easier implement in java.
my challenge how can “embed” javascript script java method. i’m java coder.
the pseudocode java method this:
<java method begins……> string input = “abc” //its more complex abc string o1= null; //javascript script begins, //javascript evaluates java string input //javascript output assigned java o1 o1 = output; //javascript script ends //maniputate , process java string o1 - not null <java method ends>
fairly simple java 8. can utilize nashorn.
import javax.script.invocable; import javax.script.scriptengine; import javax.script.scriptenginemanager; public class jstest { @org.junit.test public void test() throws exception { scriptengine jsengine = new scriptenginemanager().getenginebyname("nashorn"); jsengine.eval("var = function(name) {return 'hello ' + name;}"); invocable jsscript = (invocable) jsengine; object result = jsscript.invokefunction("say", "xyz"); system.out.println(result); } } the eval method has various possible parameters. possible, loading script file.
a tutorial can found here: http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial
java javascript tree-traversal
No comments:
Post a Comment