【发布时间】:2021-07-30 07:31:27
【问题描述】:
我有一个代码:
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);
}
}
【问题讨论】:
标签: java eval jshell printstream