How do I send output from System.out to socket e.g. PrintWriter?
I need to send whatever appears on the console output (both std and err) to PrintWriter, thus a socket.
Thank you.
How do I send output from System.out to socket e.g. PrintWriter?
I need to send whatever appears on the console output (both std and err) to PrintWriter, thus a socket.
Thank you.
May be you can try like this:
OutputStream os = socket.getOutputStream(); // for example
PrintStream ps = new PrintStream(os);
System.setOut(ps);
Well, you can, but please be more specific about your requirements. If you want to send logging messages, you'll better use some logging framework like logback where you can switch from ConsoleAppender
to SocketAppender
immediately.
You can also replace System.out
with System.setOut()
and redirect output to some other target.
If you want to redirect the whole output of some application, you can try some bash/nc
workaround as well:
$ java -jar your_app.jar | nc some_server