Okay, so I have been experimenting with the Process and Runtime classes and I have run into a problem. When I try to execute this command : cmd /c dir, the output is null. Here is a snippet of my code:
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c dir");
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
//BufferedReader serverOutputError = new BufferedReader(new InputStreamReader(serverStart.getErrorStream()));
String line = null;
while ((output.readLine()) != null) {
System.out.println(line);
}
int exitValue = process.waitFor();
System.out.println("Command exited with exit value: " + exitValue);
process.destroy();
System.out.println("destroyed");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
And I get this for an output:
(18 lines of just "null")
Command exited with exit value: 0
destroyed
Any ideas?
You never set your
linevariable that you are using to write to the console.Replace
with