java - 为什么 System.console() 如果使用 java 执行则设置,如果通过 ant 执行则取消设置?

标签 java ant console

我用 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/

相关文章:

vb.net - 我们如何在VB.Net控制台应用程序中使用计时器控件?

java - Spring MVC : How to bind to nested object properties in the @ModelAttribute

java - 如何对Java Spark RDD执行标准差和均值运算?

Java项目Ant构建错误

java - 为多平台 SWT 应用程序为每个平台构建一个 jar

javascript - 如何在多行控制台上更新数据

java - JpaPagingItemReader 无法读取数据并转到下一步

java - 斯坦福 NLP 服务器 "Could not handle incoming annotation"(德语)

java - 您如何在 Maven pom.xml(来自 Ant)中设置 java.security.policy?

ruby-on-rails - 如何在 Fargate 容器中启动 rails 控制台