java没有执行系统命令
在下面的程序中,我将名称指定为“don”,因此该命令将搜索活动目录 所有的名字都以don开头(如唐纳德等)。但是 line2 变量在从 reader 对象赋值后变为 null 并且它永远不会进入循环。我做错了什么?仅供参考:当我在命令行上给出该命令时,该命令会起作用。
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();
}
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 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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没记错的话,管道(或重定向)需要使用 cmd.exe 启动程序。
像这样的东西:
If I am not mistaken, the pipe (or redirection) requires to launch the programs with cmd.exe.
Something like:
我至少可以看到一些可能的问题:
1)作为 PhiLho 写道:管道和重定向是由 shell 完成的(sh、bash...或 Windows 上的 cmd.exe)。您必须在 Java 代码中处理它或在 shell 中运行命令。
2)调用
waitFor()
后,线程将被阻塞,直到进程终止,只有当您“消耗”它的InputStream时,进程才会终止。这不会发生,因为waitFor()
仍在等待...最好在附加线程中读取和处理 InputStream(或在读取 InputStream 后调用waitFor
)。3)关闭后阅读(最后两行)应该抛出异常。
读取ErrorStream可以帮助发现一些错误,并且还指示检查
waitFor
的返回。编辑:
实际上该代码应该抛出一些异常。
异常是被报告(
printStackTrace
)还是被忽略?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 sincewaitFor()
is still waiting... Better to read and process the InputStream in an additional Thread (or callwaitFor
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?