Is there anyway to get or intercept the output stream (System.out in particular) from a class’s method that has been invoked within my program?
I am creating a new Method object called main, using URLClassLoader to load in the class, and then calling main.invoke(); with proper arguments.
I want to be able to capture the output from that method’s execution, it is okay for it to still display to the screen, but I need to be able to process the output as well.
Note: This method is actually a server application that will run continuously.
Please let me know if any more clarification is needed, and also let me know if anything like this is even possible.
Thanks for any help!
Assuming you are talking about the System.out or System.err output streams…
The best way to implement what you suggest is to pass the OutputStream as an argument. This could be
System.outbut can easily be changed to whatever you want.A way to do this without passing an argument is to replace the default System.out with your own which checks the stack to determine where it was called from. I would suggest you do this unless there is no other option. e.g. you have no access to the code of the method you are calling.
If you are talking about some other OutputStream, you would have to replace the code for the outputstream used with one which allows you to trace the output. This is the hackiest solution but it would work.