package src;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Command
{
public static void main(String args[])
{
try
{
Process p=Runtime.getRuntime().exec("cmd /c dir");
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch(IOException e1) {}
catch(InterruptedException e2) {}
System.out.println("Done");
}
}
I tried to run this code but it is not able to execute anything. Are there some classpath or other settings i need to do to make this work?
e1.printStrackTrace();in the exception blocks to see whats wrong>javac Command.java -d .and Run it from command prompt>java src.CommandreadLine(), waiting for input or waiting at thewaitFor(). Related question was answered here: stackoverflow.com/questions/8149828/…, it has thewaitFor()after the loop