Ruby脚本系统发送NullPointerException for Java

Ruby脚本系统发送NullPointerException for Java,java,ruby,Java,Ruby,我想在我目前正在开发的Java应用程序上使用Ruby编写内容脚本,但不幸的是,我一直在系统中得到NullPointerException。假设加载一系列Ruby脚本以在Java中执行 Exception in thread "main" java.lang.NullPointerException at com.anamoly.utilities.PluginInterpreter.executePlugin(PluginInterpreter.java:60) at com.a

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

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");
    }
}

在您的类路径中是否有任何针对Ruby的实现?如果没有,您可能希望查找(也可能有其他实现)。
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();
        }
    }
}
class ExamplePlugin
  def example_function
    puts("Launching Anamoly! Sent from Ruby!")
  end
end