2

In order to get a reference to a Console in Java, one needs to write

Console console = System.console();

However, when trying to do this in an IDE, console will be initialized with null. Why is there no Console in an IDE? What was the thought process in allowing Consoles to exist while running the program directly from the terminal as opposed to running the program in an IDE?

2 Answers2

1

Citing from the javadocs:

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.

Your IDE runs jvm in a non-interactive way (no keyboard connected to the input) so console is not available there.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
0

Why u not using

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  //for reading from  console 
   String str = bf.readline();
   //for control ...
   if(str.length >0){
           do something;
    }
    //for writing to console
      String utr= something;
      bf.write(utr);

Some things like this........

Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34