Embedding JRuby - Profiles prevent loading external scripts.

So I am embedding JRuby using the org.jruby.embed.* stuff (I think it used to be called RedBridge). Everything went fine until I tried to set the profile to prevent access to the file system and to the rest of the JVM from jruby. Using the Profile.NO_FILE_CLASS (I have also tried my own custom profile) I get a NullPointerException when I try to load external scripts (through runScriplet(“require '” + name + “’”); )

Here is the code for setting up the ScriptingContainer


this.console = console;
this.stdout = new PrintWriter(new BufferedWriter(console.getInput()));
this.stdin = new BufferedReader(console.getOutput());

this.scripter = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
this.scripter.setProfile(Profile.NO_FILE_CLASS);
System.out.println(this.scripter.getSupportedRubyVersion());
this.scripter.setLoadPaths(Arrays.asList(
          new String[]{
                GENERIC_COMPUTER.toString() + File.separator + "include",
                GENERIC_COMPUTER.toString() + File.separator + "bin",
            }
            ));
this.scripter.setOutput(stdout);
this.scripter.setError(stdout);
this.scripter.setInput(stdin);
includeDir(new File(GENERIC_COMPUTER.toString() + File.separator + "include"));
nativeCommandList = includeDir(new File(GENERIC_COMPUTER.toString() + File.separator + "bin"));
//this.scripter.setProfile(Profile.NO_FILE_CLASS);

And here is the code to include files:


private String[] includeDir(File file) {
        File[] files = file.listFiles();
        String[] names = new String[files.length];
        for(int i = 0; i < files.length; i++) {
            if(files[i].isFile()) {
                String scriptName = files[i].getName().substring(0, files[i].getName().indexOf("."));
                names[i] = scriptName;
                include(scriptName);
            } else {
                includeDir(files[i]);
            }
        }
        return names;
    }
    
    private void include(String s) {
        this.scripter.runScriptlet("require '" + s + "'");
    }

And here is the Stack Trace of the NPE:

You can’t set the profile after loading the scripts since settings can’t be changed once the jruby runtime is initialized (which happens as soon as you run a script)

I’m looking for a way to include (require in ruby language) external scripts with the NO_FILE_CLASS profile (or any other that disallows file system access). Any and all suggestions are appreciated.

Thanks in advance

I don’t know any ruby but I think require is trying to access the filesystem to get the scripts. What if you remove require and instead pass the script through that runScriptlet() ?


private void include(String s) {
    BufferedReader reader = new BufferedReader(new TextReader(new File(s)));
    String code = "";

    String line;
    while ( (line = reader.readLine()) != null)
    {
        code += line + "\n";
    }

    this.scripter.runScriptlet(code);
}

Require is the ruby equivalent of Java’s import. It’s not that I’m trying to run them, I want to make the contents available to other scripts.