unten ist das Stück Code, den ich ausführen. ob ich ein Array pf string (cmd) verwende oder es ist eine einzelne Zeichenfolge Ich bekomme eine Ausnahme (siehe weiter unten), während es eine passwortlose Anmeldung am Linux-Zielsystem gibt.Verwenden von Java-Runtime (auf einem Windows-Rechner) zum Senden eines Linux-Befehls an einen Linux-Server
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;
}
Die Ausnahme:
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)
Jede Idee, was ist die Ursache?
"Das System kann die angegebene Datei nicht finden": ist 'ssh' in% PATH% Ihres Programms? – Mat
Oder Sie haben keinen 'ssh' Client auf Ihrem Windows Rechner installiert. – nobeh
Sie können den vollständigen Pfad des 'ssh' angeben, wenn er nicht in Ihrem Pfad ist. –