No stdout from Python process started by ProcessBuilder in the Java application

22 views
Skip to first unread message

corviraptor

unread,
Nov 16, 2024, 12:57:55 PM11/16/24
to Py4J Support and Comments
Hello!

I'm using py4j as a scripting layer for Java (for Minecraft modding). It's mostly working great with the exception that any Python code executed by py4j appears to have completely empty system output. 

While I know it's likely a more common use case to start the JVM from Python, I'm building and starting the Python process with ProcessBuilder in the existing Java application because I don't want to change how every user starts the application, especially fellow developers who are less technically inclined. Here's a link to the code at buildPyProcess(). I redirectHowever, "python.log" only gets the output from when the script I start Python with is imported.

To try and extract some kind of output from Python, I've tried adding code to manually flush the Python stdout to another file (at invoke_file()). Each time the code is invoked, the "python_process.log" file simply adds a newline with no text otherwise (I assume this is the normal behavior of Python when appending an empty string to a file? I'm not the most experienced Python developer), implying that there isn't anything at stdout.

To be clear, everything that isn't the standard output works as expected.

Also, is there a better way to do code formatting or markdown here?

corviraptor

unread,
Nov 16, 2024, 12:59:41 PM11/16/24
to Py4J Support and Comments, corviraptor
Apologies, I accidentally posted the conversation before I was finished. I meant to say that I redirect the python process' InputStream with redirectOutput() to a file, but the file only gets the output from when the script I start Python with is imported.

corviraptor

unread,
Nov 16, 2024, 6:29:00 PM11/16/24
to Py4J Support and Comments, corviraptor
Update: Using `inheritIO()` before actually specifying any of the redirects fixes the issue. I'm not entirely sure why because I was specifying the input, output, and error redirects before so it should effectively be the same, but maybe I just needed to inherit the input from the JVM? I'm not super sure, ProcessBuilder is a bit of an enigma to me and I'm not super experienced with Java as a language.
Reply all
Reply to author
Forward
0 new messages