How do I send output from System.out to socket eg 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.