how to execute dos commands using java [ Runtime.getRuntime().exec("cmd.exe /c ") ]

nkk1712 used Ask the Experts™
hi how to achive the following ..

try {
      String Arr[] = {"cmd", "/c","dir","cd C:\\test", "dir"};  

              Process p = Runtime.getRuntime().exec(Arr);
              OutputStream oStream = p.getOutputStream();
              BufferedWriter sWriter = new BufferedWriter(new OutputStreamWriter(oStream));
              sWriter.write("cd C:/test/hello");
              sWriter.write("dir /w");
              read(p);          } catch (IOException e) {

private static void read(Process p)
                String line = null;
              BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
              while((line=input.readLine()) != null) {

          catch(IOException e1) { e1.printStackTrace(); }
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

What exactly you are trying?


automate the build process for my porject

You can use the below code to list the file and directory:
public class Util {

    public static void main(String[] a) {
        System.out.println("Scouting folder: " + "C:\\");
        listFiles("c:/", true);

    private static void listFiles(String root, boolean iterative) {
        File[] filesList = new File(root).listFiles();
        for (int i = 0; i < filesList.length; ++i) {
                System.out.println("File name: " + filesList[i].getName());

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial