• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1697
  • Last Modified:

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

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(); }
  • 3
1 Solution
What exactly you are trying?
nkk1712Author Commented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now