Solved

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

Posted on 2012-12-23
4
1,503 Views
Last Modified: 2012-12-24
hi how to achive the following ..

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

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


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

          }
          catch(IOException e1) { e1.printStackTrace(); }
      }
0
Comment
Question by:nkk1712
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
yats earned 500 total points
ID: 38717958
0
 
LVL 6

Expert Comment

by:yats
ID: 38717962
What exactly you are trying?
0
 

Author Comment

by:nkk1712
ID: 38718042
automate the build process for my porject
0
 
LVL 6

Expert Comment

by:yats
ID: 38718069
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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question