執行命令我試圖從執行Java程序的腳本:從Java
public class TestCommandLine
{
public static void main (String[] args)
{
String PATH = "/path/programs/";
String command = PATH + "name_programs param1 param2";
executeCommand (command);
}
private static String executeCommand (String command)
{
StringBuffer output = new StringBuffer();
String line = "";
Process p;
try {
p = Runtime.getRuntime().exec (command);
p.waitFor();
BufferedReader reader = new BufferedReader (new InputStreamReader (p.getInputStream()));
while ((line = reader.readLine()) != null) {
output.append (line + "\n");
}
}
catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
沒有錯誤,但程序不運行。我也嘗試其他解決方案,從計算器,但他們都沒有工作
以一些用於調試的語句開始(或者通過適當的調試程序進行操作)。首先打印出命令字符串。如果你複製粘貼字符串,它是否可以在cmd行手動工作?你也調用'executeCommand',但不捕獲返回字符串。那個字符串是由什麼組成的?你怎麼確定程序不運行?預期的行爲是什麼? – scrappedcola
已經完成。我已經在控制檯中輸出了我的命令,將它複製並粘貼到命令行中,該命令正確工作 – Modfoo
檢查exitValue和getErrorStream()的內容。這就是錯誤會被指出的地方。 – OrangeDog