我正在尝试使用java进程执行程序运行shell脚本。当我将它用于PING命令时,它对我来说很好,但是无法对DUMP命令产生任何影响。这是我正在尝试的代码。
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ExecuteShellComand {
public static void main(String[] args) {
ExecuteShellComand obj = new ExecuteShellComand();
String domainName = "www.google.com";
String command1 = "ping -c 3 " + domainName;
String command = "pg_dump -U kalculate Kalculate_4.1_final -f testfinal11.sql";
String output = obj.executeCommand(command1);
obj.executeCommand(command);
System.out.println(output);
}
private 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");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
这里obj.executeCommand(command);
没有任何影响。任何帮助都会很明显。
答案 0 :(得分:0)
我认为你必须将参数分成单独的变量或字符串。
示例:
来自:
String command1 = "ping -c 3 " + domainName;
String command = "pg_dump -U kalculate Kalculate_4.1_final -f testfinal11.sql";
String output = obj.executeCommand(command1);
致:
String command1 = "ping";
String parameter01 = "-c";
String parameter02 = "3";
String output = obj.executeCommand(command1, parameter01, parameter02, domainName);