In Java, System.console() returns the unique java.io.Console object for the program's terminal, or null if the JVM has no console. It offers readLine, readPassword, and printf.
Whether a console exists depends on how the program is started: from a terminal it usually does; in many IDEs, with redirected input or output, or as a service, the result is traditionally null. Always check before using it.
A static method of the java.lang.System class, added in Java 6 (JDK 1.6). Call it on the class name, as in System.console(...). No import is needed, because classes in java.lang are always visible.
Syntax
Get the console and check it before use:
Java
Console console = System.console();
if (console == null) {
// no terminal: fall back to System.in / System.out
} else {
String name = console.readLine("Name: ");
}
Call
What it does
System.console()
The terminal's Console, or null.
System.console() == null
The safe check before using it.
console.readLine("Name: ")
Prints a prompt and reads one typed line.
Added in Java 6. Every call returns the same object.
Library declarations
Java
// java.lang.System
public static Console console()
Parameters
Parameter
Description
(none)
console() takes no arguments.
Return Value
Return Value
Description
Console
The system console, or null when no console is available.
Compare with Similar Methods
Other ways to talk to the user:
Method
How it differs
System.in / System.out
Always present; work in IDEs and with redirects, but cannot hide passwords.
The safest first step with System.console(): test for null and leave politely when there is no terminal.
Java
import java.io.Console;
public class SystemConsoleCheck {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
System.out.println("Console available");
System.out.println("Same object again: " + (console == System.console()));
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java SystemConsoleCheck. The last part is a run where System.console() is null (IDE, redirected input).
Console available
Same object again: true
--- When System.console() returns null ---
No console available. Run this program from a terminal.
How It Works
Ask once, store it. The result goes into the variable console. In a terminal it is a Console object; in many IDE run windows, or with java SystemConsoleCheck < in.txt, it is null.
Leaving early on null. The if prints a helpful message and returns, so no method is ever called on null. That is the second block of the output.
One console per JVM. A second System.console() call returns the identical object, so == gives true.
Example 2 — Read a Name With or Without a Console
Use the console when it exists and fall back to System.in when it does not. The user sees the same prompt either way.
Java
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
public class SystemConsoleFallback {
static String readName() throws IOException {
Console console = System.console();
if (console != null) {
return console.readLine("Your name: ");
}
System.out.print("Your name: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
return reader.readLine();
}
public static void main(String[] args) throws IOException {
String name = readName();
System.out.println("Hello, " + name + "!");
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java SystemConsoleFallback. (user types ...) marks keyboard input, not program output. Without a console the output looks the same.
Your name: Ana (user types: Ana)
Hello, Ana!
How It Works
Two routes to one answer. With a console, console.readLine("Your name: ") prints the prompt and reads the line in one call. Without one, the code prints the prompt with System.out.print and reads with a BufferedReader.
Works in an IDE too. The fallback reads System.in, which IDEs and pipes provide, so echo Ana | java SystemConsoleFallback greets Ana too.
End of input. Both readLine methods return null if input ends first (Ctrl+D or Ctrl+Z), which would print "Hello, null!".
Example 3 — Print a Table Through the Console Writer or System.out
Pick one PrintWriter, the console's or a wrapper around System.out, and let the rest of the program use it without caring which one it got.
Java
import java.io.Console;
import java.io.PrintWriter;
public class SystemConsoleWriter {
static PrintWriter output() {
Console console = System.console();
if (console != null) {
return console.writer();
}
return new PrintWriter(System.out, true);
}
public static void main(String[] args) {
PrintWriter out = output();
out.printf("%-6s%5s%n", "Item", "Qty");
out.printf("%-6s%5d%n", "Pens", 12);
out.printf("%-6s%5d%n", "Books", 3);
out.flush();
System.out.println("Console used: " + (System.console() != null));
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java SystemConsoleWriter. The last part is a run where System.console() is null (IDE, redirected input).
Choosing the writer.output() returns console.writer() when a console exists, otherwise a PrintWriter around System.out with auto-flush turned on.
Same columns both ways.%-6s pads names on the right to 6 characters and %5d pads numbers on the left to 5, so "Pens" plus 12 becomes "Pens 12" whichever writer is used.
Only the last line differs. After flush(), the program reports which route it took: true in a terminal, false when System.console() is null.
Frequently Asked Questions
Many IDE run windows are not real terminals, so the JVM has no console; redirection has the same effect. Run from a terminal, or fall back to System.in and System.out.
Plain Java has no hidden-input alternative. Ask the user to run from a terminal, or read System.in and accept visible typing.
Yes. Since Java 22 some JDK versions return a Console in more situations, and Console.isTerminal() tells whether it is a real terminal.
Yes. It returns the one console of the JVM, or null, so repeated calls give the same result.
No. The Console reads from the terminal directly, so a stream installed with setIn is ignored by it.