java is not executing system command

前端 未结 2 1151
感动是毒
感动是毒 2021-01-22 05:37

In the following program am giving name as \"don\" so the command will search activedirectory with all the names starting with don (like donald etc). But the line2 variable beco

相关标签:
2条回答
  • 2021-01-22 05:59

    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");
    
    0 讨论(0)
  • 2021-01-22 06:13

    I can see at least some possible problems:

    1) as PhiLho wrote: pipe and redirection is done by the shell (sh, bash,... or cmd.exe on Windows). You must handle it in the Java code or run your commands in a shell.

    2) after calling waitFor() the Thread is blocked until the process terminates, the process only terminates if you "consume" it's InputStream. This is not happening since waitFor() is still waiting... Better to read and process the InputStream in an additional Thread (or call waitFor after reading the InputStream).

    3) reading after closing (2 last lines) should throw an Exception.

    Reading the ErrorStream could help find some errors, and checking the return of waitFor is also indicated.

    EDIT:
    actually there should be some Exceptions being throw by that code.
    Are the Exceptions being reported (printStackTrace) or just ignored?

    0 讨论(0)
提交回复
热议问题