Skip to content

Instantly share code, notes, and snippets.

@dejw
Created July 23, 2009 22:23
Show Gist options
  • Select an option

  • Save dejw/153687 to your computer and use it in GitHub Desktop.

Select an option

Save dejw/153687 to your computer and use it in GitHub Desktop.
package jsolver;
import java.io.*;
/* Klasa pozwalajaca na uruchamianie programow. */
public class Invoker {
/** Uruchamia polecenie systemowe.
* Wymusza, aby proces sie zakonczyl, zanim funkcja powroci z wynikiem.
*
* @param command polecenie do uruchomienia
*/
public static int command(String command, boolean quiet) {
boolean err = false;
try {
Process process = new ProcessBuilder(command.split(" ")).start();
String s;
if(!quiet){
BufferedReader results = new BufferedReader(new InputStreamReader(process.getInputStream()));
while((s = results.readLine())!= null) System.out.println(s);
}
/* Bledy za zawsze wyswietlane */
BufferedReader errors = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((s = errors.readLine())!= null) {
System.err.println(s);
err = true;
}
if(err) throw new InvokerException("Blad poczas uruchamiania " + command);
return process.waitFor();
} catch(Exception e){
throw new RuntimeException(e);
}
}
public static int command(String command){ return command(command, false); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment