Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now