Description of the problem
A somewhat contrived example illustrating my question. Imagine that we have a library of javascript functions, which is already supported and updated daily by the army of frontend developers. To be specific, imagine that one of these functions looks like this:
function employeesForStore(store) { var dictionary = { "downtown": ["Joe", "Mary", "Steve"], "uptown": ["Jules", "Vincent", "Matt"],
NOTE. Please ignore the implementation details of this function. The actual function can be much more complicated than a simple search in the JSON dictionary, and suppose we do not know any details about the implementation of the js function. We only know that it takes a String argument and returns an array of arrays.
Now we would like to use this feature in our Java code. That is, in our Java code, we would like to βloadβ this function, and then be able to call it several times, passing it String args and getting the results String[] or ArrayList<String> .
From searching SO and google so far, I understand that this will be related to using:
javax.script.ScriptEngineManagerjavax.script.ScriptEngine- and possibly
scriptEngine.getContext() to pass values ββto a function and get the results.
I'm a little hazy about the details of the above, especially since most of the examples I found include running javascript code at a time, instead of using the javascript function for Java.
Sample code that I would like to see
- Assuming the js function is in the file "my_functions.js", load this file in Java so that all its functions are available for use.
- Call
employeesForStore("downtown") and save its results in native java String[] or List<String> in a variable named downtownResults . - Same as 2, but call
employeesForStore("uptown") and save uptownResults in the variable
Jonah source share