Skip to content

Instantly share code, notes, and snippets.

@LearnItCodeIt
Created November 25, 2016 20:59
Show Gist options
  • Save LearnItCodeIt/7a21cd121048cbb2025c1c9d2a407701 to your computer and use it in GitHub Desktop.
Save LearnItCodeIt/7a21cd121048cbb2025c1c9d2a407701 to your computer and use it in GitHub Desktop.
the output of this method is the same output of the command variable if you typed in cmd because it create a cmd process and input the command variable then take the output and return it
import java.io.BufferedReader;
import java.io.InputStreamReader;
class CommandPrompt {
public static void runSystemCommand(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = "";
// reading output stream of the command
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment