java - Redirecting Jruby ScriptEngine output to a StringWriter - Stack Overflow

java - Redirecting Jruby ScriptEngine output to a StringWriter - Stack Overflow


2
down vote

 

This appears to work (I'm using 1.6.1, released last week):

    ScriptEngineManager factory = new ScriptEngineManager();     ScriptEngine engine = factory.getEngineByName("jruby");     StringWriter sw = new StringWriter();     PrintWriter pw = new PrintWriter(sw);     engine.getContext().setWriter(pw);     engine.eval("puts 'hello'");     System.out.println("output: " + sw.getBuffer()); 

你可能感兴趣的:(redirect)