我用 Java 编写了一个小的命令行应用程序,并想为此使用新类 java.io.Console。我使用 System.console() 来获取此类的实例。如果我通过“java -jar MyApp.jar”调用我的应用程序,但如果我通过 ant 的 java 任务执行应用程序,则此调用返回一个工作控制台。对于此调用,fork 为真,spwan 为假。为什么会有这种差异(System.out.print() 在 ant 下工作正常)?如果我通过 ant 启动我的应用程序,我该如何使用控制台?
最佳答案
this method 的 Javadoc状态:
Returns the unique Console object associated with the current Java virtual machine, if any.
以及 System.Console
的文档类状态:
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.
我可以想象,当 Ant fork 一个新的 Java 进程时,它会重定向标准输出。
关于java - 为什么 System.console() 如果使用 java 执行则设置,如果通过 ant 执行则取消设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1199987/