Java卡在Windows Server 2003上的WMIC命令执行的无限循环中 [英] Java stuck in infinite loop executing a wmic command on Windows Server 2003

查看:74
本文介绍了Java卡在Windows Server 2003上的WMIC命令执行的无限循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Windows Server 2003计算机上正在运行的进程及其文件路径的列表.我正在使用以下代码来尝试做到这一点:

I'm trying to get a list of running processes and their file paths on a Windows Server 2003 machine. I'm using the following code to try and do that:

protected Map<String,String> getProcesses() {
    Map<String,String> processes = new HashMap<String,String>();
    try {
        String line;
        Process p = null;

        // Windows
        if (OS.indexOf("win") >= 0) {
            p = Runtime.getRuntime().exec("wmic process get description,executablepath");
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            LOG.info("Entering while loop");
            while ((line = input.readLine()) != null) {
                LOG.info("blah");
                String[] array = line.split("\\s+");
                if (array.length > 1) {
                    processes.put(array[0], array[1]);
                }
            }
            LOG.info("Exited while loop");
            input.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return processes;
}

程序在while条件下陷入无限循环."blah"和退出循环时退出"永远不会输出到日志中.我已经在我的win7本地计算机和输出信息的服务器上的命令提示符中运行了命令.我还在本地计算机上运行了上面的代码,它也可以正常工作.在过去三个小时的谷歌搜索中,似乎找不到Java和Windows Server 2003之间的问题.任何帮助将不胜感激.

The program gets stuck in an infinite loop at the while condition. "blah" and "Exited while loop" never output to the log. I've ran the command in command prompt on both my win7 local machine and the server which outputs the information just fine. I've also ran the above code on my local machine which also works fine. It looks like it's some issue between Java and Windows Server 2003 that I haven't been able to find in the past 3 hours of googling. Any help would be much appreciated.

推荐答案

在获取和使用 InputStream 之前,您需要获取并关闭 OutputStream .这将向您确认已经开始,已经完成了向流程发送输入(在这种情况下,没有输入).

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();

请记住,在 Process 对象上, getInputStream()输入来自进程的输出流,而 getOutputStream()输出进入到流程的输入流.

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卡在Windows Server 2003上的WMIC命令执行的无限循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆