Capture Python's stdout in Java?

306 views
Skip to first unread message

brad

unread,
Jul 5, 2018, 7:51:57 PM7/5/18
to Jep Project
Jep *and* Python newbie here; pardon if this is an odd question:

I'm trying to capture stdout from Jep from within a Java program. I've successfully used JepConfig().setRedirectOutputStreams(true) and see, for instance "hi" in IntelliJ's debug console when I exec("print("hi")").

But when I try to grab Java's stdout stream so I can get that output by doing this:
ByteArrayOutputStream pipeOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(pipeOut));
it stops displaying "hi" in the console, but never gets it into pipeOut.

Am I doing something wrong? Or (better) is there a more direct way to receive stdout (& stderr) in a Java program running Python through Jep?

Thanks!
Brad

brad

unread,
Jul 5, 2018, 8:07:01 PM7/5/18
to Jep Project
Note that a more direct grabbing of the streams is better: I prefer not to mix Java's outputs with Pythons--only did this because I couldn't find a more direct way.

Nathan Jensen

unread,
Jul 9, 2018, 12:09:18 PM7/9/18
to Jep Project
If you called System.setOut(...) after instantiating the Jep with redirectStreams=true, then that Jep's sub-interpreter would still have a reference to the previous stream of System.out.  If you call System.setOut(...) before instantiating the Jep with redirectStreams=true, then I'd expect it to work.  Can you confirm the order of operations you did?

On Thu, Jul 5, 2018 at 7:07 PM, brad <br...@didi.co> wrote:
Note that a more direct grabbing of the streams is better: I prefer not to mix Java's outputs with Pythons--only did this because I couldn't find a more direct way.

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/59594cfd-1bb5-4a56-92ac-6bd876e5b2c2%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages