为什么控制台在Eclipse的IntelliJ或总是空? [重复](Why Console in

2019-09-29 11:33发布

可能重复:
System.Console()返回null

码:

public class Demo {

public static void main(String[] args){
    Console c = System.console();
    if (c == null) {
        System.err.println("No console.");
        System.exit(1);
    }else {
        System.out.println("Console is.");
        System.exit(0);
    }
}

}

始终没有控制台。 为什么呢? 怎么修? 谢谢。

Answer 1:

You don't have any console associated.

As per javadoc

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

EDIT:

From Console javadoc.

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.



文章来源: Why Console in Eclipse or IntelliJ always null? [duplicate]