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

时间:2015-09-14 13:11:32

标签: java runtime.exec sshpass

为什么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/

1 个答案:

答案 0 :(得分: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");
}
相关问题