gpt4 book ai didi

java - Java 中的后台进程

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:29 25 4
gpt4 key购买 nike

我一直在准备 Java 7 的 OCP 考试。我正在阅读的这本书遇到了一些我完全不理解的东西。

也就是说,

If the JVM is invoked indirectly by IDE, or if the JVM is invoked from a background process, then the method call System.console() will fail and return null.

在什么情况下以及如何从后台进程调用 JVM?有人可以详细说明吗?最好的问候

最佳答案

假设您在 UNIX 系统上运行一个程序,该程序可以请求两个操作数并产生它们的和。如果您将其调用为:

/home/ucas> java -jar add.jar
Please enter the first summand:
42
Please enter the second summand:
17
The sum is 59.

现在假设您在后台运行它:

/home/ucas> java -jar add.jar &
java.lang.NullPointerException at Add.main(Add.java:17)
....

查看 Add.java,您会看到:

Console console = System.console(); // 16
Reader reader = console.reader(); // 17

进程与终端分离,因此 console 将为 null。堆栈跟踪打印到标准错误,未重定向。

关于java - Java 中的后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17615681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com