设置特定 JShell 的输出流

问题描述

我有一个代码:

public class App {
    public static void main(String[] args) {
        System.out.println("Hello World from main!");
        JShell shell = JShell.builder().build();
        shell.eval("System.out.println(\"Hello World from JShell!\");");

    }
}

现在我希望我只能为 JShell 设置输出流,而不是普通代码。

我试过了:

shell.eval("System.setOut(new Printer());");
shell.eval("System.out.println(\"Hello World!\");");

但是不起作用!

我的打印机类:

class Printer extends PrintStream{
    public Printer() {
        super(System.out);
    }

    @Override
    public void print(String s) {
        super.print("Message: - " + s);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)