Rhino沒有標準輸入/輸出的概念,因此您必須將Writer指定給某個變量。像這樣:
import java.io.StringWriter;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
public class SetWriter {
public static void main(String[] args) {
Context c=Context.enter();
ScriptableObject scope = c.initStandardObjects();
StringWriter writer=new StringWriter();
ScriptableObject.putProperty(scope, "writer", writer);
String source = " writer.write('hello'); ";
c.evaluateString(scope, source, "TEST", 1, null);
System.out.println(writer.getBuffer());
}
}
或者,你在你的腳本中使用System.out.println,可以考慮使用System.setOut運行腳本之前。