Ruby Scripting System为Java发送NullPointerException

时间:2014-12-19 06:31:36

标签: java ruby

我想在我正在使用的Java应用程序上使用Ruby编写脚本内容,但不幸的是我一直在使用系统获得NullPointerException。它假设加载一系列要在Java中执行的Ruby脚本。

Exception in thread "main" java.lang.NullPointerException
    at com.anamoly.utilities.PluginInterpreter.executePlugin(PluginInterpreter.java:60)
    at com.anamoly.Launcher.main(Launcher.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 

Launcher.java

package com.anamoly;

import com.anamoly.utilities.PluginInterpreter;

/**
 * @author Braeden Dempsey <https://github.com/bdemps98>
 */
public class Launcher {
    private static PluginInterpreter interpreter = new PluginInterpreter();

    public static void main(String[] args) {
        interpreter.loadAllPlugins();
        interpreter.executePlugin("example_function");
    }
}

PluginInterpreter.java

package com.anamoly.utilities;

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;

/**
 * @author Braeden Dempsey <https://github.com/bdemps98>
 */
public class PluginInterpreter {
    private ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    private ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("ruby");

    private static final String PLUGIN_DIRECTORY = "/data/plugins/";

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

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

        for (String i : pathCollection) {
            loadPlugin(i);
        }
    }

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

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

            if (children != null) {
                for (File child : children) {
                    if (child.isFile() && child.getName().endsWith(".rb")) {
                        try {
                            scriptEngine.eval(new InputStreamReader(new FileInputStream(child)));
                        } catch (FileNotFoundException | ScriptException exception) {
                            exception.printStackTrace();
                        }
                    } else if (child.isDirectory()) {
                        loadAllPlugins();
                    }
                }
            }
        }
    }

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

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

example_plugin.rb

class ExamplePlugin
  def example_function
    puts("Launching Anamoly! Sent from Ruby!")
  end
end

0 个答案:

没有答案