下面是我正在運行的代碼片段。無論我使用數組pf字符串(cmd)還是它是 單個字符串,我都會在進行無密碼登錄到目標Linux系統時出現異常(請參閱下面的內容)。使用java運行系統(在Windows機器上)發送linux命令到linux服務器
private static int bringHostFile() {
try {
String[] cmd ={"ssh" , "[email protected]" , "/root/bring_hosts"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
String s = null;
// read the output from the command
if ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
return 0;
}
例外:
java.io.IOException: Cannot run program "ssh": CreateProcess error=2, The system cannot find the file specified.
at java.lang.ProcessBuilder.start(ProcessBuilder.java:471)
at java.lang.Runtime.exec(Runtime.java:604)
at java.lang.Runtime.exec(Runtime.java:442)
at java.lang.Runtime.exec(Runtime.java:339)
at JavaRunCommand.CommandGetCurrentCPUSize(JavaRunCommand.java:140)
at EC.<init>(EC.java:29)
任何想法的原因是什麼?
「系統找不到指定的文件」:在程序的%PATH%中是'ssh'? – Mat 2012-03-30 13:28:07
或者,您的Windows計算機上沒有安裝'ssh'客戶端。 – nobeh 2012-03-30 13:29:04
如果'ssh'不在你的路徑中,你可以指定'ssh'的完整路徑。 – 2012-03-30 16:26:08