Q
Q
Quad_Tree2016-06-08 08:44:23
JavaScript
Quad_Tree, 2016-06-08 08:44:23

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()); // класс ядра из проекта

И при выполнении следующего JS скрипта:
core.isRun();
JS движок выдаёт ошибку: isRun is not function
Хотя в тоже время, без ошибок выполняет данный скрипт:
out.println("Hello");
Так чем отличается метод в классе проекта от метода в System.out? И как это можно исправить?
P.S. Метод выполнения самого скрипта:
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

2 answer(s)
S
Severus256, 2016-06-08
@severus256

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

N
Nick Smith, 2016-06-09
@Braidner

Core.getInstance()
Must return a public class with public moths

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question