【发布时间】:2023-10-14 21:27:01
【问题描述】:
我正在编写一个需要执行 shell 命令的 java 程序,所以我编写了一个函数,它将命令作为字符串执行(即:“mkdir ~/Folder1”)并使用 shell 执行该命令。这是函数:
private static void shell(String cmd)
{
try
{
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line = buf.readLine()) != null) {
System.err.println(line); // show any errors returned by the command executed on the error console
}
} catch (Exception ee) {}
}
由于某种奇怪的原因,这个函数没有执行任何命令。我做错了吗?执行shell命令似乎是一件简单的事情,但它不起作用。
【问题讨论】:
-
在您的
catch块中,执行以下操作:ee.printStackTrace();并发布错误 -
我看到的,是一个10声望,1金1银的家伙……:P
-
为什么会有不同?
标签: java shell command execute