0

I am struggling to invoke javascript function from java. My problem comes from the fact that whole function is actually received as a string argument:

public void selectMethod(Map<T> item, String function)

where function object is actually the whole java script method. I need to call this method with nashorn

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

I need to pass the function with map item as well. When js function modifies the map object I have to print it out in java.

I would appriciate any help.

1 Answer 1

1

You can use invokeFunction method on Invocable [ https://docs.oracle.com/javase/8/docs/api/javax/script/Invocable.html#invokeFunction-java.lang.String-java.lang.Object...- ]

Nashorn engine implements javax.script.Invocable interface. From your "selectMethod" java method, you could do something like:

((Invocable)engine).invokeFunction(function, item);

I assume the script function name passed is that of a global script function [which was already defined in engine via "eval" call done earlier].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.