Solved

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

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
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…

630 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