You need to read InputStream from the process, here is an example:

Edit I modified the code as suggested here to receive the errStream with the stdInput

ProcessBuilder builder = new ProcessBuilder("command goes here");
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream is = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

String line = null;
while ((line = reader.readLine()) != null) {
   System.out.println(line);
}

For debugging purpose, you can read the input as bytes instead of using readLine just in case that the process does not terminate messages with newLine

Answer from iTech on Stack Overflow
Top answer
1 of 5
146

Here are the primary differences between using System.out/.err/.in and System.console():

  • System.console() returns null if your application is not run in a terminal (though you can handle this in your application)
  • System.console() provides methods for reading password without echoing characters
  • System.out and System.err use the default platform encoding, while the Console class output methods use the console encoding

This latter behaviour may not be immediately obvious, but code like this can demonstrate the difference:

public class ConsoleDemo {
  public static void main(String[] args) {
    String[] data = { "\u250C\u2500\u2500\u2500\u2500\u2500\u2510", 
        "\u2502Hello\u2502",
        "\u2514\u2500\u2500\u2500\u2500\u2500\u2518" };
    for (String s : data) {
      System.out.println(s);
    }
    for (String s : data) {
      System.console().writer().println(s);
    }
  }
}

On my Windows XP which has a system encoding of windows-1252 and a default console encoding of IBM850, this code will write:

???????
?Hello?
???????
โ”Œโ”€โ”€โ”€โ”€โ”€โ”
โ”‚Helloโ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”˜

Note that this behaviour depends on the console encoding being set to a different encoding to the system encoding. This is the default behaviour on Windows for a bunch of historical reasons.

2 of 5
16

They're essentially the same, if your program is run from an interactive prompt and you haven't redirected stdin or stdout:

public class ConsoleTest {
    public static void main(String[] args) {
        System.out.println("Console is: " + System.console());
    }
}

results in:

$ java ConsoleTest
Console is: java.io.Console@2747ee05
$ java ConsoleTest </dev/null
Console is: null
$ java ConsoleTest | cat
Console is: null

The reason Console exists is to provide features that are useful in the specific case that you're being run from an interactive command line:

  • secure password entry (hard to do cross-platform)
  • synchronisation (multiple threads can prompt for input and Console will queue them up nicely, whereas if you used System.in/out then all of the prompts would appear simultaneously).

Notice above that redirecting even one of the streams results in System.console() returning null; another irritation is that there's often no Console object available when spawned from another program such as Eclipse or Maven.

๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-print-output-to-console-in-java-417652
How to print output to console in Java | LabEx
Learn how to print basic and advanced console output in Java programming. Discover techniques to display text, variables, and formatted data in the console.
๐ŸŒ
CSVeda
csveda.com โ€บ home โ€บ java features and advantages โ€บ writing console output in java
Writing Console Output in Java - CSVeda
April 29, 2022 - Alternatively, you can make use of the write() method for directing the output of your program to the console. The easiest syntax of the write() method is: ... Where b is an integer of low order eight bits. Let us have a look at a simple example. class writeEg { public static void main(String args[]) { int a, b; a = 'Q'; b = 65; System.out.write(a); System.out.write('\n'); System.out.write(b); System.out.write('\n'); } } ... You wonder: โ€œCan I pay someone to do my Java homework for me?
๐ŸŒ
Princeton CS
introcs.cs.princeton.edu โ€บ java โ€บ 15inout
1.5 Input and Output - Introduction to Programming in Java
September 18, 2022 - Standard output. To print output values in our programs, we have been using System.out.println(). Java sends the results to an abstract stream of characters known as standard output. By default, the operating system connects standard output to the terminal window.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ print-methods-in-java
Print Methods in Java โ€“ How to Print to the Terminal
April 19, 2023 - If you want to print something in the terminal, you need to use one of the print methods. There are actually three different print methods in Java. They are the print, printf, and println methods.
๐ŸŒ
Hashnode
bharathkalyans.hashnode.dev โ€บ how-to-style-java-console-output
How to Style Java Console Output? - Bharath Kalyan's Blog
January 7, 2023 - public class StylingConsoleOutput { public static void main(String[] args) { System.out.println("Plain Simple Output ๐Ÿ˜•"); System.out.println(Color.BLACK + "" + Color.YELLOW_BACKGROUND + "Hello, I am Bharath!" + Color.RESET); System.out.println(Color.CYAN_BOLD + "Styled Text!"
Find elsewhere
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ random โ€บ how to print output to console in java
How to Print output to Console in Java
April 3, 2025 - Without parentheses, Java thinks you want to concatenate strings, not add numbers. I've fallen into this trap myself! Sometimes, you need your output to look neat and tidy, like when you're printing a table or formatting numbers.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 49433712 โ€บ how-to-print-the-shell-script-terminal-output-in-java
bash - How to print the shell script terminal output in java - Stack Overflow
March 22, 2018 - Sorry, new to java coding ... sudo will read a password from the current terminal. You'd have to set up a PTY and duplex communication to it, which Java doesn't have standard APIs for (though you can run it through script instead which incidentally does set up a pty).
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ essential โ€บ io โ€บ cl.html
I/O from the Command Line (The Javaโ„ข Tutorials > Essential Java Classes > Basic I/O)
The Java platform supports three Standard Streams: Standard Input, accessed through System.in; Standard Output, accessed through System.out; and Standard Error, accessed through System.err. These objects are defined automatically and do not need to be opened.
๐ŸŒ
DEV Community
dev.to โ€บ awwsmm โ€บ coloured-terminal-output-with-java-57l3
Coloured Terminal Output with Java - DEV Community
October 21, 2018 - I was looking for a way to add a bit of flair to some terminal output today and found this StackOverflow post explaining how to add colour to terminal output using ANSI colour codes. Here's what the output from the below Java code looks like in my (MobaXterm / Ubuntu) shell:
๐ŸŒ
Carleton University
cs.carleton.edu โ€บ faculty โ€บ dmusican โ€บ cs117s03 โ€บ iocheat.html
Java Console and File Input/Output Cheat Sheet
PrintWriter out = new PrintWriter(new FileWriter("K:\\location\\outputfile.txt"))); out.print("Hello "); out.println("world"); out.close();
๐ŸŒ
Puc-rio
web.tecgraf.puc-rio.br โ€บ ~ismael โ€บ Cursos โ€บ apostilas โ€บ Java-Notes โ€บ examples-introductory โ€บ console โ€บ console-output.html
Java: Console Output
The System class is automatically imported (as are all java.lang classes). ... You can write one complete output line to the console by calling the System.out.println() method. The argument to this method will be printed. println comes from Pascal and is short for "print line".
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java io โ€บ read and write user input in java
Read and Write User Input in Java | Baeldung
January 8, 2024 - Letโ€™s use the println() method to print a String and terminate the line: System.out.println("Please enter your name and surname: "); Alternately, we can use the print() method, which works similarly to println(), but without terminating the ...
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java examples โ€บ read and write to console with examples
Read and Write to Console with Examples
October 1, 2022 - In this Java tutorial, learn different ways to read from and write to the system console.
๐ŸŒ
CodeJava
codejava.net โ€บ java-se โ€บ file-io โ€บ java-console-input-output-examples
Java Console Input Output Examples
The following program demonstrates how to use the Console class to read input data from the user and print output: import java.io.*; import java.util.*; /** * This program demonstrates how to use the Console class to read input * and write output in command-line interface.