Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过java执行时,ldapsearch返回错误_Java_Ldap - Fatal编程技术网

通过java执行时,ldapsearch返回错误

通过java执行时,ldapsearch返回错误,java,ldap,Java,Ldap,我通过java执行了ldapsearch命令。请参阅下面的代码 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class TestMain { public static void main(String[] args) throws InterruptedException, IOException { S

我通过java执行了ldapsearch命令。请参阅下面的代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestMain
{

    public static void main(String[] args) throws InterruptedException, IOException
    {
        String ldspCmd ="ldapsearch -ZZ -h ldap-url.com -x -D cn=username,ou=webapps,ou=ec,o=uoa -w $(echo PassWord | base64 -di) -b ou=ec_users,dc=ec,dc=auckland,dc=ac,dc=nz \"(groupMembership=cn=bpmusers,ou=ec_group,dc=ec,dc=auckland,dc=ac,dc=nz)\"";
        String output = executeCommand(ldspCmd);

        System.out.println(output);

    }

    private static String executeCommand(String command) {

        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }

            BufferedReader errorReader =
                    new BufferedReader(new InputStreamReader(p.getErrorStream()));

            String erroLine = "";
            while ((erroLine = errorReader.readLine())!= null) {
                output.append(erroLine + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();
    }
}
但是我得到了错误“ldapsearch:无法解析调试值“I”

但当我通过命令行执行相同的命令时,它会正确执行并返回记录

我做错了什么?有人能帮我排序吗?

参数列表中的结构(如
$(echo PassWord | base64-di)
由shell解释和处理。当您使用
Runtime.exec
从Java调用命令时,您不是在使用shell,而是直接将命令传递给操作系统,因此shell无法解释这些结构

如果希望获得这些好处,则需要显式调用shell。 此外,Java没有shell那样的复杂逻辑来将参数拆分为命令。Java只是在空格处剪切参数列表

因此,在
executeCommand
方法中有一行:

p = Runtime.getRuntime().exec(command);
您应该将其更改为:

// Add shell invocation around the above command
String[] shellCommand = { "/bin/bash", "-c", command };

p = Runtime.getRuntime().exec(shellCommand);
参数列表中的
$(echo PassWord | base64-di)
之类的结构由shell解释和处理。当您使用
Runtime.exec
从Java调用命令时,您不是在使用shell,而是直接将命令传递给操作系统,因此shell无法解释这些结构

如果希望获得这些好处,则需要显式调用shell。 此外,Java没有shell那样的复杂逻辑来将参数拆分为命令。Java只是在空格处剪切参数列表

因此,在
executeCommand
方法中有一行:

p = Runtime.getRuntime().exec(command);
您应该将其更改为:

// Add shell invocation around the above command
String[] shellCommand = { "/bin/bash", "-c", command };

p = Runtime.getRuntime().exec(shellCommand);

如何通过Java执行命令?请发布一个命令-如果你不显示你在做什么,当然不可能跟踪你的问题。我发布的命令正是我使用的。我删除了密码和ldap url。我在这里尝试的是搜索“bpmusers”组中的用户。发布您的Java代码使用带有密码的引号,如
-w“$(echo password | base64-di)”
@marabu没有区别。同样的结果。如何通过Java执行命令?请发布一个命令-如果你不显示你在做什么,当然不可能跟踪你的问题。我发布的命令正是我使用的。我删除了密码和ldap url。我在这里尝试的是搜索“bpmusers”组中的用户。发布您的Java代码使用带有密码的引号,如
-w“$(echo password | base64-di)”
@marabu没有区别。同样的结果。这个效果很好。谢谢很抱歉,因为我正忙于其他任务,所以回复太晚。这很有效。谢谢很抱歉,因为我正忙于其他任务,所以回复晚了。