Java System console() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

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: ");
}
CallWhat it does
System.console()The terminal's Console, or null.
System.console() == nullThe 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

ParameterDescription
(none)console() takes no arguments.

Return Value

Return ValueDescription
ConsoleThe system console, or null when no console is available.

Compare with Similar Methods

Other ways to talk to the user:

MethodHow it differs
System.in / System.outAlways present; work in IDEs and with redirects, but cannot hide passwords.
console.reader()The console's own Reader for character input.
console.writer()The console's PrintWriter for output.

Java Examples

Example 1 — Check Whether a Console Exists

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()));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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 + "!");
    }
}

How It Works

  1. 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.
  2. Works in an IDE too. The fallback reads System.in, which IDEs and pipes provide, so echo Ana | java SystemConsoleFallback greets Ana too.
  3. 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));
    }
}

How It Works

  1. Choosing the writer. output() returns console.writer() when a console exists, otherwise a PrintWriter around System.out with auto-flush turned on.
  2. 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.
  3. 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.

Next: currentTimeMillis()

The current wall-clock time in milliseconds since January 1, 1970 UTC.

currentTimeMillis() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful