I’m running an application from inside another one for testing purposes. I want to redirect the output for the tested app to a file, so I can have a log after each test.
Is there a way to redirect the output of an app to a file from the command line in java?
You can use the output stream redirector that is supported by the Windows command line, *nix shells , e.g.
Alternatively, as you are running the app from inside the vm, you could redirect
System.outfrom within java itself. You can use the methodSystem.setOut(PrintStream ps)
Which replaces the standard output stream, so all subsequent calls to System.out go to the stream you specify. You could do this before running your wrapped application, e.g. calling
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt"))));If you are using a wrapper that you can’t modify, then create your own wrapper. So you have FEST wrapper -> stream redirector wrapper -> tested app.
For example, you can implement a simple wrapper like this:
You invoke it with 3 additional params – the Main class to run, and the output/error directs.