Created
June 29, 2011 02:42
-
-
Save ytkhs/1052855 to your computer and use it in GitHub Desktop.
a java tips like "shell_exec()" on PHP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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