下面是我正在运行的代码片段。我是否使用数组 pf string (cmd) 或者它是 当对目标 Linux 系统进行无密码登录时,我会得到一个字符串异常(请参阅下文)。
private static int bringHostFile() {
try {
String[] cmd ={"ssh" , "root@im6-64s" , "/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;
}
异常(exception):
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)
知道原因是什么吗?
最佳答案
关于java - 使用java运行时(在Windows机器上)将Linux命令发送到Linux服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9943899/