Solved

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

Posted on 2012-12-23
4
1,494 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 43
CSV file parsing thru Java 13 34
SequenceInputStream example 3 19
How to configure empty element in XML Document parser? 15 36
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

861 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