import java.io.IOException; /** A simple static class to launch a process remotely Example: ProcessLauncher.launch("devilbunny", "/usr/local/bin/ls") */ public class ProcessLauncher { public static void launch(String machine, String realCmd) { String cmd = null; try { cmd = SSH_PATH + machine + " " + realCmd + " 2>&1"; System.out.println("Launching: "+ cmd); Process p = Runtime.getRuntime().exec(cmd); try { // wait for exit code -- if it's 0, command worked, int exitCode = p.waitFor(); } catch(InterruptedException x) { System.err.println("Error launching cmd='" + cmd + "'"); System.err.println("Caught exception: " + x); } } catch(IOException x) { System.err.println("Could not launch cmd=" + cmd); System.err.println("Caught exception: " + x); } } /** * Simple example. */ public static void main(String[] args) { launch("devilbunny", "/usr/local/bin/ls > "+ CWD + "/ls.out"); launch("trouble", "/usr/local/bin/touch " + CWD + "/newFile"); } // The absolute pathname of ssh. private static final String SSH_PATH = "/usr/local/bin/ssh "; // Current working directory private static String CWD = System.getProperty("user.dir", ""); }