2

I'm currently writing a rest API in java that handles different processes, and what I'm trying to achieve is redirecting the output of a python process while it is running indefinitely to a .txt file. This is my current code:

File log = createLog(name);
File environment = new File(path);
Process process = new ProcessBuilder(exe,cmd,args).redirectOutput(log).directory(environment).start();

but nothing is written to the created log file. I know the process is being run, since if I switch redirectOuput with inheritIO the java process prints what the python process prints. The log file is also created successfully. What am I doing wrong, and how do I do it correctly?

Edit: To be clear, the process never finishes and i want the output of the process to continuously be piped to the log file.

1 Answer 1

1

Assuming your code is these statements then your program will terminate without waiting for the sub-process to finish. Add a Process.waitFor() call. Like,

Process process = new ProcessBuilder(exe,cmd,args).redirectOutput(log)
        .directory(environment).start();
process.waitFor();
Sign up to request clarification or add additional context in comments.

4 Comments

The problem is that it is a process that never finishes, and I want to pipe the output continuously to the log file. Sorry if that wasn't clear enough.
@viloz Every process eventually finishes. You want your Java program to keep running until then. Which is what this will do.
This one doesn't. As I said, I'm constructing a rest server that handles http requests and thus the java process will run indefinetly until I choose to turn it off (or it crashes). It never ends, and all the proccess starts are triggered by http requests. The python process is of similiar characteristics, it never ends unless I tell it to or it crashes. I don't want the java program to stall while it waits for the process to finish, since it needs to handle other http requests in the meantime. I think I need to implement some sort of parallellism, but I'm unsure how to
Rethink your entire architecture. You shouldn't be spawning a python process to handle a webservice call from Java. cgi-bin is not scalable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.