java没有执行系统命令

java没有执行系统命令

本文介绍了java没有执行系统命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序中,我将名称命名为don,因此命令将搜索activedirectory
,其中所有名称都以don开头(如donald等)。但是,从reader对象赋值后,line2变量变为null,并且它永远不会进入循环。我究竟做错了什么?仅供参考:当我在命令行上给它时,该命令有效。

In the following program am giving name as "don" so the command will search activedirectorywith all the names starting with don (like donald etc). But the line2 variable becomes null after the assignment from reader object and it never goes into the loop. What am i doing wrong? FYI: the command works when i give it on the command line.

try {
    Process p = Runtime.getRuntime().exec(
        "dsquery user -name " + name + "* -limit 200|dsget user -samid -display");
    p.waitFor();
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(p.getInputStream()));
    String line2 = reader.readLine();
    HashMap<String,String> hmap = new HashMap<String,String>();
    while (line2 != null) {
        line2 = line2.trim();
        if (line2.startsWith("dsget")||line2.startsWith("samid")) {
            continue;
        }
        String[] arr = line2.split(" ",1);
        hmap.put(arr[0].toLowerCase(),arr[1].toLowerCase());
        line2 = reader.readLine();
    }
    reader.close();
    line2 = reader.readLine();
}


推荐答案

如果我没弄错的话,管道(或重定向)需要使用cmd.exe启动程序。
类似于:

If I am not mistaken, the pipe (or redirection) requires to launch the programs with cmd.exe.Something like:

Process p = Runtime.getRuntime().exec("cmd /c dsquery user -name " + name + "* -limit 200|dsget user -samid -display");

这篇关于java没有执行系统命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:51