This is a bug #122429 of eclipse

Answer from swimmingfisher on Stack Overflow
🌐
Coderanch
coderanch.com › t › 641563 › java › System-console-returns-null
System.console() returns null (Java in General forum at Coderanch)
The problem is that System.console() returns null, and for that reason the NullPointerException is thrown at 11. Is there a way to run this in Eclipse without significant changes in the code?.
🌐
Eclipse
bugs.eclipse.org › bugs › show_bug.cgi
122429 – System.console() (Java 6.0) returns null when running inside Eclipse
Bugzilla – Bug 122429 System.console() (Java 6.0) returns null when running inside Eclipse Last modified: 2021-09-17 03:34:03 EDT
🌐
GitHub
github.com › ConvertAPI › convertapi-java › issues › 5
System.console() is null when using an IDE · Issue #5 · ConvertAPI/convertapi-library-java
June 4, 2019 - Caused by: java.lang.NullPointerException at com.convertapi.client.Http.getRequestBuilder(Http.java:62) at com.convertapi.client.Param.lambda$upload$1(Param.java:98) Possible fix: static Request.Builder getRequestBuilder() { if (System.console() == null) { System.out.printf("VERSIJA: %s", Http.class.getPackage().getImplementationVersion()); } else { System.console().printf("VERSIJA: %s", Http.class.getPackage().getImplementationVersion()); } String agent = String.format("ConvertAPI-Java/%.1f (%s)", Http.class.getPackage().getImplementationVersion(), System.getProperty("os.name")); return new Request.Builder().header("User-Agent", agent); } No one assigned ·
Author   ConvertAPI
🌐
GitHub
github.com › google › error-prone › issues › 4355
SystemConsoleNull should consider the case where `System.console() == null` · Issue #4355 · google/error-prone
March 29, 2024 - However, it may still return null, both from the specification and actual implementation. ... @SuppressWarnings("SystemConsoleNull") // https://errorprone.info/bugpattern/SystemConsoleNull private static boolean systemConsoleIsTerminal() { Console systemConsole = System.console(); if (Runtime.version().feature() < 22) { return systemConsole != null; } try { return (systemConsole != null) && (Boolean) Console.class.getMethod("isTerminal").invoke(systemConsole); } catch (ReflectiveOperationException e) { throw new LinkageError(e.getMessage(), e); } }
Author   google
🌐
Stack Overflow
stackoverflow.com › questions › 23541565 › console-console-system-console-gives-null
java - Console console = System.console(); gives null - Stack Overflow
As stated in the Javadoc for System.console(), it can return null if there is no console allocated for the JVM. In fact this is often the case.
Top answer
1 of 2
3

If you start java from a terminal window, then it really should work, even though I haven't tried on OSX.

If you run a simple test using java directly from the terminal, does it work?

echo 'public class Test { public static void main(String[] args) {System.console().printf("hello world%n");}}' >Test.java && javac Test.java && java Test 

Expected output:

hello world

If it doesn't work, then sorry, no console support on your platform.

However, if it works, and your program doesn't then there is a problem with how your program is started.

Check how the java binary started? Is it started from a shell script? Check that stdin/stdout have not been redirected or piped into something, and possibly also that it's not started in the background.

ex: This will probably make System.console() return null.

java Test | tee >app.log    

and this:

java Test >/tmp/test.log

This seems to work on my machine (linux)

java Test &

Neither does it seem as if System.setOut, System.setErr or System.setIn affects the console, even after a couple of gc's and finalizers.


However:

Closing (the original) System.out or System.in will disable the console too.

echo 'public class Test { public static void main(String[] args) {System.out.close();System.console().printf("hello world%n");}}' >Test.java && javac Test.java && java Test 

Expected output:

Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:1)

So; scan your code for places where it closes streams, or passes System.out somewhere it might get closed.

2 of 2
1

To read from Standard input (command line input) you must use some kind of stream reader to read the System.in stream. An InputStreamReader initialised by

    InputStreamReader(System.in) 

lets you read character by character. However, I suggest wrapping this with a BufferedReader:

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String inputLine = reader.readLine();

Must import

    java.io.*;
Top answer
1 of 5
4

From the Javadoc:

Returns the unique Console object associated with the current Java virtual machine, if any.

If there is no console associated to the JVM, the pointed line is the call of a method on a null object, hence the exception.

How do you launch your application?

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If you want to read the username from the standard input, you could use this code:

try {
    System.out.print("Enter Username: ");
    InputStreamReader streamReader = new InputStreamReader(System.in);
    BufferedReader bufferedReader = new BufferedReader(streamReader);
    String username = bufferedReader.readLine();
} catch (IOException e) {
    e.printStackTrace();
}
2 of 5
3

That is because System.console()is returning null. The official documentation states (bold is mine to emphasize):

public static Console console()

Returns the unique Console object associated with the current Java virtual machine, if any.

Returns: The system console, if any, otherwise null.

You can see it here.

Find elsewhere
🌐
Mkyong
mkyong.com › home › java › java – how to read input from system.console()
Java - How to read input from System.console() - Mkyong.com
January 31, 2020 - System.console() returns null in an IDE, try running it in the console or terminal. $ cd project/target/classes project/target/classes$ java com.mkyong.io.JavaSample Enter your name: mkyong Name is: mkyong Enter your password: Password is: 123456 · Console JavaDoc ·
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › Console.html
Console (Java Platform SE 8 )
April 21, 2026 - The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. ... A string containing the line read from the console, not including any line-termination characters, or null if an end of stream has been reached.
🌐
JetBrains
youtrack.jetbrains.com › issue › SCL-22246 › System.console-is-null
System.console is null : SCL-22246
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
JetBrains
youtrack.jetbrains.com › issue › IDEABKL-5949 › System.console-returns-null
System.console() returns null : IDEABKL-5949
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8296454
[JDK-8296454] System.console() shouldn't return null in jshell
That includes the provision of a Console through System.console() to use jshell's REPL to tinker with java.io.Console. ACTUAL - The actual result is that in jshell, System.console() returns null, which makes using jshell's REPL to tinker with java.io.Console impossible.
🌐
Tabnine
tabnine.com › home › code library
java.lang.System.console java code examples
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
Coderanch
coderanch.com › t › 385139 › java › java › System-Console-receives-null
Why System.Console receives null? (Java in General forum at Coderanch)
No, it is normal for Eclipse not to associate a Console. This question comes up once every few weeks; there is usually only a Console object if you start your application from a command line or shell or terminal. It is in the javadoc documents which Ulf quoted yesterday.
Top answer
1 of 3
4

Stephen C's answer: "Read the Java 22 release notes. There is a section that explains what has changed ... and what to do rather than testing if 'console()' returns 'null.'" Helped me. He left it as a comment so I'm replying with an answer to make sure it's preserved and so people have to do less manual digging but the credit goes to him.

The JDK 22 release notes say that System.console() will no longer return null when the standard streams are redirected or connected to a virtual terminal. Instead the Console.isTerminal() method allows you to check if the console is connected to a terminal.

Console console = System.console();
if(!console.isTerminal()) {
    //code goes here...
}

Additionally running with -Djdk.console=java.base will restore the old functionality

2 of 3
0

It looks like System.console() is not behaving as expected in your environment, possibly because of how the JVM or IDE is configured. Sometimes, when running Java applications from IDEs or certain environments, System.console() may not return null even if the application isn't actually being run from a terminal.

Here’s an alternative approach to determine if your Java application is running in a terminal or not:

Using System.getenv() and System.getProperty()

You can use environment variables and system properties to infer whether your application is running in a terminal.

  • On Windows, the presence of the COMSPEC environment variable typically indicates a terminal or command prompt environment.
  • On Unix-like systems, the presence of the TERM environment variable suggests that the application might be running in a terminal.
private static boolean isRunningInTerminal() {
        // Check if environment variables typically set for terminals are present
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("windows")) {
            String comSpec = System.getenv("COMSPEC");
            return comSpec != null && !comSpec.isEmpty();
        } else {
            String term = System.getenv("TERM");
            return term != null && !term.isEmpty();
        }
    }

You could also check for the presence of specific system properties or runtime configurations that are indicative of running in a terminal environment. add additional debugging outputs to understand the environment better:

System.out.println("Console: " + System.console());
System.out.println("COMSPEC: " + System.getenv("COMSPEC"));
System.out.println("TERM: " + System.getenv("TERM"));
System.out.println("OS Name: " + System.getProperty("os.name"));
System.out.println("OS Version: " + System.getProperty("os.version"));
System.out.println("Java Version: " + System.getProperty("java.version"));
System.out.println("User Directory: " + System.getProperty("user.dir"));
System.out.println("Current Working Directory: " + System.getProperty("user.dir"));

This will help you determine what environment variables and properties are available, and why System.console() might not be returning null.

🌐
Solved
code.i-harness.com › en › q › 40247e
java - returns - what does console null mean - Solved
System.console() returns null if there is no console. You can work round this either by adding a layer of indirection to your code or by running the code in an external console and attaching a remote debugger.