简体   繁体   中英

Call internal javascript function from java code

I have to call a javascript function named function startPolling() and created into hill.js file (/Demo MM/src/main/webapp/static/assets/js/hill.js) from java code. How can i do?Thanks I'm using this code but receive exception

ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    if (!(engine instanceof Invocable)) {
      System.out.println("Invoking methods is not supported.");
      return "500";
    }
    Invocable inv = (Invocable) engine;
    String scriptPath = "/Demo MM/src/main/webapp/static/assets/js/hill.js";

    try {
        engine.eval("load('" + scriptPath + "')");
    } catch (ScriptException e) {
        return "500";
    }

Make sure you are in the correct working directory to begin with. I get your code working.

To output working directory:

System.out.println("Working Directory = " +
          System.getProperty("user.dir"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM