慕后森
您可以使用Windows命令行* nix shells支持的输出流重定向器,例如java -jar myjar.jar > output.txt另外,当您从vm内部运行应用程序时,可以System.out从java本身内部进行重定向。您可以使用方法System.setOut(PrintStream ps)它将替换标准输出流,因此所有对System.out的后续调用都将转到您指定的流。您可以在运行打包的应用程序之前执行此操作,例如调用System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt"))));如果您使用的包装程序无法修改,请创建自己的包装程序。因此,您具有FEST包装器->流重定向器包装器->经过测试的应用程序。例如,您可以实现一个简单的包装器,如下所示:public class OutputRedirector{ /* args[0] - class to launch, args[1]/args[2] file to direct System.out/System.err to */ public static void main(String[] args) throws Exception { // error checking omitted for brevity System.setOut(outputFile(args(1)); System.setErr(outputFile(args(2)); Class app = Class.forName(args[0]); Method main = app.getDeclaredMethod("main", new Class[] { (new String[1]).getClass()}); String[] appArgs = new String[args.length-3]; System.arraycopy(args, 3, appArgs, 0, appArgs.length); main.invoke(null, appArgs); } protected PrintStream outputFile(String name) { return new PrintStream(new BufferedOutputStream(new FileOutputStream(name)), true); }}您使用3个附加参数调用它-要运行的Main类,然后输出/错误指示。