Why Console in Eclipse or IntelliJ always null? [duplicate]

痞子三分冷 提交于 2019-12-24 07:59:32

问题


Possible Duplicate:
System.Console() returns null

Code:

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

}

always No console. Why ? How to fix? Thanks.


回答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.



来源:https://stackoverflow.com/questions/13423256/why-console-in-eclipse-or-intellij-always-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!