The available answers provide enough information to call javascript code using Nashorn, specially the duplicated one that suggests 31piy in the first comment.
Then I assume that your real problem is to reach the javascript file that contains your code, present in your webapp folder. I can see some possible answers to that:
Here some fellows suggest possible ways to reach the file using Spring methods: Accessing files/directories in webapp folder in Spring You should consider if there is a way available to your particular framework
However: is there a good reason for that file to be in your webapp folder? Is its logic necessary at all in the client side? Since you need the logic in the server side, I would not (even for prudence) expose it to the client of your application. If it is not necessary in the client side, don't expose it. Just keep it, let's say, in your application's src/main/resources directory (assuming you build it with maven or gradle) and access it cleanly from your classloader:
// Access the resource from the classloader
try {
reader = new InputStreamReader(
YourServletOrWhateverClass.class.getClassLoader().
getResourceAsStream("your_javascript_file.js"),
Charset.forName("UTF-8"));
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval(reader);
Invocable inv = (Invocable) engine;
inv.invokeFunction("your_function");
}
catch(NoSuchMethodException nsme) {
nsme.printStackTrace();
}
catch(ScriptException se) {
se.printStackTrace();
}
finally {
try {
reader.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}