JavaScript Scripting System为Java发送NullPointerException

时间:2014-12-18 15:31:53

标签: java javascript

我想使用JavaScript为我的Java应用程序编写内容脚本,并且遇到了我的脚本系统的一些问题。系统假设要加载一系列要在Java中执行的脚本,但是当我尝试加载脚本时遇到以下错误:

Exception in thread "main" sample-plugin.js is loaded?
java.lang.NullPointerException
    at com.javascript.ScriptHandler.loadScript(ScriptHandler.java:47)
    at com.javascript.ScriptHandler.loadScriptsFromDirectory(ScriptHandler.java:33)
    at com.javascript.Main.main(Main.java:8)

Main.java

package com.javascript;

public class Main {

    private static ScriptHandler scriptHandler = new ScriptHandler();

    public static void main(String[] args) {
        scriptHandler.loadScriptsFromDirectory("./data/scripts/");
    }

}

ScriptHandler.java

package com.javascript;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.ArrayList;

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

public class ScriptHandler {

    private static ScriptEngineManager scriptManager;
    private static ScriptEngine scriptEngine;

    public ScriptHandler() {
        scriptManager = new ScriptEngineManager();
        scriptEngine = scriptManager.getEngineByName("nashorn-javascript");
    }

    public void loadScriptsFromDirectory(String directory) {
        ArrayList<String> pathCollection = new ArrayList<String>();
        String[] path = { "skill", "player" };

        for (String i : path) {
            pathCollection.add(directory + i + "/");
        }

        for (int i = 0; i < pathCollection.size(); i++) {
            loadScript(pathCollection.get(i));
        }
    }

    private final void loadScript(String directory) {
        File file = new File(directory);

        if (file.exists() && file.isDirectory()) {
            File[] children = file.listFiles();

            for (File child : children) {
                if (child.isFile() && child.getName().endsWith(".js")) {
                    try {
                        System.out.println(child.getName() + " is loaded?");
                        scriptEngine.eval(new InputStreamReader(new FileInputStream(child)));
                    } catch (FileNotFoundException | ScriptException exception) {
                        exception.printStackTrace();
                    }
                } else if (child.isDirectory()) {
                    loadScriptsFromDirectory("./data/scripts/");
                }
            }
        }
    }

    public void executeScriptFunction(String function, Object... objects) {
        Invocable invocable = (Invocable) scriptEngine;

        try {
            invocable.invokeFunction(function, objects);
        } catch (NoSuchMethodException | ScriptException exception) {
            exception.printStackTrace();
        }
    }
}

采样plugin.js

var plugin = function() {
    print("Hello from JavaScript")
}

1 个答案:

答案 0 :(得分:2)

根据this example,nashorn引擎名称仅为nashorn而非nashorn-javascript