Created
November 29, 2018 04:07
-
-
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.
This file contains 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
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) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
waitFor() is not really needed, I just feel that father's thread should not die until son's process ends