Skip to content

Instantly share code, notes, and snippets.

@ytkhs
Created June 29, 2011 02:42
Show Gist options
  • Save ytkhs/1052855 to your computer and use it in GitHub Desktop.
Save ytkhs/1052855 to your computer and use it in GitHub Desktop.
a java tips like "shell_exec()" on PHP.
try {
Process ps = Runtime.getRuntime().exec("ps");
BufferedReader reader = new BufferedReader(new InputStreamReader(ps.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
ps.waitFor();
System.out.println(output.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
ps.destroy();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment