java - 从 Java 调用 WMIC.EXE(挂起)

标签 java windows subprocess

我正在尝试从 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 your InputStream. 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, and getOutputStream() output goes to the input stream of the process.

关于java - 从 Java 调用 WMIC.EXE(挂起),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33812951/

相关文章:

java - 如何为许多用户使用 modeshape

java - 包装默认的 servlet 但覆盖默认的 webapp 路径

windows - Windows 中最后运行的进程的 PID

windows - 如何在传递给 Powershell 中的函数时在参数中添加引号

python - 为什么在将 Unicode 写入 CMD 时会出现 IOErrors? (代码页 65001)

java - 让 IntelliJ 识别 AnnotationProcessor 生成的类

java - JTable 单元格中的 JTree,很少有问题

python - Python:subprocess.call,stdout到文件,stderr到文件,在屏幕上实时显示stderr

python - 如何在 psutil.Popen 完成后但在终止之前获取进程运行的 cpu_times

python - block - 将输入发送到 python 子进程管道