1

I am working on a legacy app which depends on user command line input:

String key = System.console().readLine("Please enter the license key: ");

However, I am getting a NullPointerException because System.console() gives me a null.

Why does System.console() return null for a command line app? It happens when running it out of the terminal as well as IDE.

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • "The system console, if any, otherwise null." how do i eliminate the "otherwise" scenario? – amphibient Jan 07 '14 at 21:52
  • 2
    What terminal, what OS? – KarlP Jan 07 '14 at 21:54
  • OSX using the Terminal app. IDE = IntelliJ – amphibient Jan 07 '14 at 21:59
  • IntelliJ [doesn't seem to handle the console properly](http://stackoverflow.com/questions/13423256/why-console-in-eclipse-or-intellij-always-null). – KarlP Jan 07 '14 at 22:07
  • same happens with the Terminal... – amphibient Jan 07 '14 at 22:09
  • According to the API: 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. – E A Aug 22 '16 at 06:42

2 Answers2

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.

KarlP
  • 5,149
  • 2
  • 28
  • 41
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.*;
Saad Attieh
  • 1,396
  • 3
  • 21
  • 42
  • This has nothing to do with standard input. The question is asking why System.console() returns null when the program is run from the command line. – VGR Jan 07 '14 at 22:34
  • 1
    @VGR It is just providing an alternative that is more likely to work,. As also mentioned in http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console The bufferedReader approach is more reliable as it will work on IDEs and does not depend on where java is run from. – Saad Attieh Jan 08 '14 at 06:37