The issue is not whether the process is in the foreground or background. When you start a process using Java, you have to use Java to control that process' lifecyle. The Java API provides you access to various attributes of the process. What you're interested in here is the output of the process. That is represented by the process' InputStream. It seems counterintuitive, but it makes sense because from the perspective of your Java program, the process' output is the program's input. Conversely, if you need to send data to the process, you write to the process' OutputStream.

To sum up, access the process' InputStream and print that out to the command-line:

Process process = new ProcessBuilder("C:\\Path\\To\\My\\Application.exe").start();

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    output.append(line);
}

System.out.println(line);

This code, of course, assumes that your process is not waiting for any input, i.e., it is not interactive.

Answer from Vivin Paliath on Stack Overflow
Top answer
1 of 2
3

The issue is not whether the process is in the foreground or background. When you start a process using Java, you have to use Java to control that process' lifecyle. The Java API provides you access to various attributes of the process. What you're interested in here is the output of the process. That is represented by the process' InputStream. It seems counterintuitive, but it makes sense because from the perspective of your Java program, the process' output is the program's input. Conversely, if you need to send data to the process, you write to the process' OutputStream.

To sum up, access the process' InputStream and print that out to the command-line:

Process process = new ProcessBuilder("C:\\Path\\To\\My\\Application.exe").start();

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    output.append(line);
}

System.out.println(line);

This code, of course, assumes that your process is not waiting for any input, i.e., it is not interactive.

2 of 2
0

Vivin Paliath's answer is really the way to go, then you can do whatever you want with the output, display it in your own dialogue, log it, interpret it, check for errors or whatever.

But just in case you really want that command window showing up. Execute cmd.exe and get the process' OutputStream and write the command (application.exe) to it ending with a new line.

Something along the lines of:

Process p = new ProcessBuilder("C:\\Windows\\System32\\cmd.exe").start();
out = p.getOutputStream();
out.write("path\\application.exe\r\n".getBytes());
out.flush();

Should usually drain the input stream too though anyway.

Discussions

processbuilder - How to run a proccess completely seperate in Java - Stack Overflow
Try to use Process.getOutputStream, ... you can run in the background also. ... I don't want to wait for the Ruby Script to finish executing before exiting from Java. How can you start a process in the background from Java? ... you can add nohup or & in your command array to take the process in background in java. ProcessBuilder builder = ... More on stackoverflow.com
🌐 stackoverflow.com
Sending commands to a background executable via Java ProcessBuilder in MATLAB
Hello, I am using Java ProcessBuilder in MATLAB to open an executable in the background. This executable does some automated tasks, then pauses, waiting for user to type in some text and press enter. I am able to send "enter key" commands to the executable via ProcessBuilder, but I am unable to send text strings. Can someone help me in this? Here is my code so far that initiates the executable in the background: ... After running ... More on mathworks.com
🌐 mathworks.com
0
0
November 24, 2015
process - Keeping java subprocess alive in background - Stack Overflow
While child thread (deamon) will continue to run until Process that it started finishes ... The problem you are having is that subprocess that you ran has finished by the time you are trying to 'talk' to it. In your situation you are doing inter-process communication and what Java allows (from perspective of using ProcessBuilder ... More on stackoverflow.com
🌐 stackoverflow.com
Start shell script in background from java program - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most · Connect and share knowledge within a single location that is structured and easy to search More on stackoverflow.com
🌐 stackoverflow.com
🌐
Mkyong
mkyong.com › home › java › java processbuilder examples
Java ProcessBuilder examples - Mkyong.com
January 19, 2019 - package com.mkyong.process; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import java.util.concurrent.*; import java.util.stream.Collectors; public class ProcessBuilderExample2 { public static void main(String[] args) { ExecutorService pool = Executors.newSingleThreadExecutor(); ProcessBuilder processBuilder = new ProcessBuilder(); // Run this on Windows, cmd, /c = terminate after this run processBuilder.command("cmd.exe", "/c", "ping -n 3 google.com"); try { Process process = processBuilder.start(); System.out.println("proc
🌐
MathWorks
mathworks.com › matlabcentral › answers › 257278-sending-commands-to-a-background-executable-via-java-processbuilder-in-matlab
Sending commands to a background executable via Java ProcessBuilder in MATLAB - MATLAB Answers - MATLAB Central
November 24, 2015 - Hello, I am using Java ProcessBuilder in MATLAB to open an executable in the background. This executable does some automated tasks, then pauses, waiting for user to type in some text and press enter. I am able to send "enter key" commands to the executable via ProcessBuilder, but I am unable to send text strings. Can someone help me in this? Here is my code so far that initiates the executable in the background: ... After running the code above, 'my_executable_program.exe' starts running in the background successfully.
🌐
CodingTechRoom
codingtechroom.com › question › how-to-run-java-process-in-background
How to Run a Java Process in the Background - CodingTechRoom
Use the `&` operator in Unix/Linux to run the process in the background. Utilize `nohup` to keep the process running even after logging out of the session. Employ Java's `ProcessBuilder` class to start processes programmatically.
🌐
Stack Overflow
stackoverflow.com › questions › 34186475 › keeping-java-subprocess-alive-in-background
process - Keeping java subprocess alive in background - Stack Overflow
While child thread (deamon) will continue to run until Process that it started finishes ... The problem you are having is that subprocess that you ran has finished by the time you are trying to 'talk' to it. In your situation you are doing inter-process communication and what Java allows (from perspective of using ProcessBuilder (PB)) is to communicate via Input/Output Streams - those you need to setup before you start the process.
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8168608
(process) ProcessBuilder should provide method to start a ...
There are times when it would be useful to start a background process that outlives the lifetime of the initiating process. This is currently not possible with the existing API, in any of ProcessBuilder, Process or ProcessHandle.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › q › 47535180
Creating a background process in java - Stack Overflow
November 28, 2017 - You can also stop this from running by using the command ... Will this thread be alive when its parent process is stopped? I think not. And how to refrence this timeline from any other java code/thread
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › ProcessBuilder.html
ProcessBuilder (Java Platform SE 8 )
April 21, 2026 - Modifying a process builder's attributes will affect processes subsequently started by that object's start() method, but will never affect previously started processes or the Java process itself. Most error checking is performed by the start() method. It is possible to modify the state of an object so that start() will fail. For example, setting the command attribute to an empty list will not throw an exception unless start() is invoked. Note that this class is not synchronized. If multiple threads access a ProcessBuilder instance concurrently, and at least one of the threads modifies one of the attributes structurally, it must be synchronized externally.
🌐
Blogger
davidrupp.blogspot.com › 2012 › 03 › starting-java-process-in-background.html
David Rupp's Blog: Starting a Java Process in the Background from Clojure
May 3, 2012 - The thing is, sh will hang around and wait until the process finishes, whether or not it's in the background. I'm not sure just why this is. sh uses Java's Runtime.exec() to do its magic; I may update it to use ProcessBuilder.start() instead and see if that helps.
🌐
Alvin Alexander
alvinalexander.com › java › java-exec-processbuilder-process-3
Java exec - execute system processes with Java ProcessBuilder and Process (part 3) | alvinalexander.com
August 3, 2017 - Putting everything together, this code, running in a Java thread, lets your program read the stdout and stderr streams from the command you're running. As mentioned in that JavaWorld program, this is very important, because if you don't read these streams in Java Threads, your system call may block.
🌐
Baeldung
baeldung.com › home › java › core java › guide to java.lang.process api
Guide to java.lang.Process API | Baeldung
June 10, 2025 - @Test void givenAProcess_whenUsingWaitFor_thenNoExceptionThrown() { // Code that interacts with a process should not throw IllegalMonitorStateException. assertDoesNotThrow(() -> { Process process = new ProcessBuilder("notepad.exe").start(); int exitCode = process.waitFor(); }); } In summary, we can fix the IllegalMonitorStateException: Current thread is not owner of the lock! when dealing with java.lang.Process, by using Process.waitFor() to wait for the process to complete. We should avoid calling wait() or notify() directly on the Process object unless we have a clear need and are handling synchronization correctly. When this method is run then the current thread won’t wait for the subprocess to get terminated or destroyed, however, it will throw an IllegalThreadStateException if the subprocess isn’t terminated.
🌐
Ongres
ongres.com › blog › java-processes-and-streams
OnGres | Java, Processes and Streams
ZT Process Executor is a much more ... 1.6+ ProcessBuilder and Process. In particular it offers an interface to watch for process input, output and other events, and a wrapper for Java Future. Still, the implementation mimics the one used by Apache Commons Exec when it comes to handle reading and writing of the process' stdin, stdout and stderr, with 3 additional threads running in background...
🌐
CloudCompare
cloudcompare.org › cloudcompare website › board index › developers › mac os related topics
Grab command line mode output with Java ProcessBuilder - CloudCompare forum
February 28, 2021 - open -a CloudCompare.app --args -NO_TIMESTAMP -C_EXPORT_FMT LAS -O /Users/s/cc/pcl_1.las -O /Users/s/cc/pcl_2.las -MERGE_CLOUDS It opens the Command Line Mode Window, like the Java approach. An additional "-SILENT" let the app run in the background, (I can see the merged point cloud), but no ...
🌐
Leo3418
leo3418.github.io › 2021 › 06 › 20 › java-processbuilder-stdout.html
Properly Handling Process Output When Using Java’s ProcessBuilder - Leo3418's Personal Site
That should be enough for an introduction which I would not need to write if I had not been inactive for about seven consecutive weeks. Let us move on to the actual topic of this post, which is a subtle bug related to Java’s ProcessBuilder that I came across in my project: if the standard output of the process created by a ProcessBuilder is not consumed, then the process can hang forever when certain conditions are met.