Remove unnecessary nested try/finally (#417)

* Remove unnecessary nested try/finally

* This handles the case of your concern.

An even better solution would be to have SSHClient and Session implement Auto-Closable so then you don't have to worry about doing anything in the finally block!
This commit is contained in:
Tom Caflisch
2018-06-11 00:54:26 -07:00
committed by Jeroen van Erp
parent 42c52e4fe6
commit 80d93ae8e7

View File

@@ -20,15 +20,17 @@ public class Exec {
try { try {
ssh.authPublickey(System.getProperty("user.name")); ssh.authPublickey(System.getProperty("user.name"));
final Session session = ssh.startSession(); final Session session = ssh.startSession();
try {
final Command cmd = session.exec("ping -c 1 google.com"); final Command cmd = session.exec("ping -c 1 google.com");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString()); System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS); cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus()); System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally { } finally {
try {
session.close(); session.close();
} catch (IOException e) {
// Do Nothing
} }
} finally {
ssh.disconnect(); ssh.disconnect();
} }
} }