1

为什么 runtime.exec() 没有执行。当我给出 sshpass 命令时

rt.exec("sshpass -p sbsiz scp '/home/surendra/Desktop/remote_backup.txt' root@192.168.59.115:/home/");

但是当我直接在终端中运行这个命令时,它就像在终端中一样工作

sshpass -p sbsiz scp '/home/surendra/Desktop/remote_backup.txt' root@192.168.59.115:/home/
4

1 回答 1

2

您可以检查 Process 类中的输入流和错误流(返回 rt.exec),以查看由于未执行命令而导致的实际错误,如下所示:

public static void printStream(InputStream is, String type){
try
   {
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);
      String line=null;
      while ( (line = br.readLine()) != null)
            System.out.println(type + ">" + line);    
   } catch (IOException ioe){
           ioe.printStackTrace();  
   }
}

public static void main(String args[])
{
    String cmd = "command to execute";
    Process proc = Runtime.getRuntime().exec("sshpass -p sbsiz scp '/home/surendra/Desktop/remote_backup.txt' root@192.168.59.115:/home/");
    printStream(proc.getInputStream(), "OUTPUT");
    printStream(proc.getErrorStream(), "ERROR");
}
于 2015-09-14T13:21:27.327 回答