Execute a system command: Difference between revisions

no edit summary
No edit summary
Line 1:
{{Task|Programming environment operations}}
 
In this task, the goal is to run either the <code>ls</code> (<code>dir</code> on Windows) system command, or the <code>pause</code> system command.
 
=={{header|Ada}}==
Line 105:
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<java>import java.util.Scanner;
import java.io.*;
 
public class Program {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("cmd /C dir");
Scanner sc = new Scanner(p.getInputStream());
while (sc.hasNext()) System.out.println(sc.nextLine());
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
}</java>
 
{{works with|Java|1.4+}}
There are two ways to run system commands. The simple way, which will hang the JVM (I would be interested in some kind of reason). -- this happens because the the inputStream buffer fills up and blocks until it gets read. Moving your .waitFor after reading the InputStream would fix your issue (as long as your error stream doesn't fill up)
Anonymous user