Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-12-23
4
Medium Priority
?
1,597 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 1500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

879 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