Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Main {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");

        if (!(engine instanceof Invocable)) {
            System.out.println("Interface implementation in script" + "is not supported.");
            return;
        }
        Invocable inv = (Invocable) engine;
        String scriptPath = "c:/Java_Dev/cal.js";
        engine.eval("load('" + scriptPath + "')");
        Calculator calc = inv.getInterface(Calculator.class);
        if (calc == null) {
            System.err.println("Calculator interface " + "implementation not found.");
            return;
        }
        double x = 2.0;
        double y = 1.0;
        double addResult = calc.add(x, y);

        System.out.println(addResult);
    }
}