Answer the question
In order to leave comments, you need to log in
How to call a method from a Java class in JavaScript?
I have added a JS engine to my Java project to execute scripts as the program runs. (I have no experience with JavaScript, so I'm sorry if the question seems obvious to you). And I link two classes to the JS engine through the Bindings class:
engine = new ScriptEngineManager().getEngineByName("javascript");
Bindings binds = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
binds.add("out", System.out); // стандартный вывод
binds.add("core", Core.getInstance()); // класс ядра из проекта
core.isRun();
isRun is not function
out.println("Hello");
public static String run(String command) {
String result = "error";
try {
result = engine.eval(command).toString();
} catch (ScriptException | NullPointerException e) {
e.printStackTrace();
}
if (result != null)
return result;
else return "Nothing to show";
}
Answer the question
In order to leave comments, you need to log in
As I understand it, you want to call backend methods in Java from the frontend?
then you need to look towards Rest controllers and AngularJS, for example
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question