Skip to content

Instantly share code, notes, and snippets.

@CansecoDev
Created November 29, 2018 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CansecoDev/103f1c0a4f33b1cc7c07d0bebc4029ed to your computer and use it in GitHub Desktop.
Save CansecoDev/103f1c0a4f33b1cc7c07d0bebc4029ed to your computer and use it in GitHub Desktop.
Java snippet for running a cmd with java and getting son's output.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SonCommand {
public static void main(String[] args) {
try {
Process pSon = Runtime.getRuntime().exec("cmd /c dir");
BufferedReader brSon = new BufferedReader(new InputStreamReader(pSon.getInputStream()));
String line;
while((line = brSon.readLine()) != null){
System.out.println(line);
}
pSon.waitFor();
} catch (IOException ex) {
} catch (InterruptedException ex) {
}
}
}
@CansecoDev
Copy link
Author

waitFor() is not really needed, I just feel that father's thread should not die until son's process ends

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment