我正在尝试从 Java 调用 WMIC.EXE 来获取电池信息。
这里是 JavaX(一种扩展的 Java 方言)的源代码:http://tinybrain.de/1001824
要查看等效的 Java 源代码,请查看 here (确实,前 60 行很重要)。
您可以使用 x30.jar 运行此程序- 或者只需编译并运行 Java source .
基本上它只是生成一个进程(通过 .bat 文件)并获取其输出。 “反引号”功能适用于其他所有功能,包括 Windows 上的功能。只是 WMIC 不适用 - 一切都会挂起。
运行命令是这样的:
WMIC Path Win32_Battery Get EstimatedChargeRemaining /Format:List
在命令提示符下运行它效果很好...只是在 Java 中不行。 Java 对于子进程来说确实很奇怪。
有什么想法吗?谢谢...
最佳答案
我相信这是 https://stackoverflow.com/a/13367685/3196753 的重复项.
引用:
You will need to get and close your
OutputStream
before getting and using yourInputStream
. That will confirm to the process that you've started that you have finished sending input (in this case, no input) to the process.p.getOutputStream().close();
Remember that on the
Process
object,getInputStream()
input comes from the output stream of the process, andgetOutputStream()
output goes to the input stream of the process.
关于java - 从 Java 调用 WMIC.EXE(挂起),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33812951/