Monday, July 21, 2014

How to execute a window command from Java

This java code will show how can we execute a windows command from Java

package commons.utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExecuteCommandFromJava {
public static void main(String[] args) throws IOException {
   String[] command = new String[3];
        command[0] = "cmd";
        command[1] = "/c";
        command[2] = "E: && dir && cd snap";
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
        }
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        String Error;
        while ((Error = stdError.readLine()) != null) {
            System.out.println(Error);
        }
        while ((Error = stdInput.readLine()) != null) {
            System.out.println(Error);
        }
}
}

10 comments:

  1. They were impressed with their resourcefulness,what is a design firm collaborative approach to development, and extensive technical knowledge.

    ReplyDelete
  2. The writer understand better the mind of people what they want to learn through their writing therefore this article is outstanding. Thanks!!!
    url opener free tool

    ReplyDelete
  3. If your system used a torsion spring, you need to call in a professional. This is because such a garage entrance opener is large and mounted over the door and you may hurt yourself while trying to perform the garage door repair. emergency garage door repair

    ReplyDelete
  4. I was reading some of your content on this website and I conceive this internet site is really informative ! Keep on putting up.
    Emergency Garage Door Repair

    ReplyDelete
  5. This specific seems to be definitely excellent. These very small facts are produced using wide range of qualifications know-how. I favor the idea a good deal Smart contract developer in new delhi

    ReplyDelete
  6. This article is unique and interesting. thanks for your share.
    Auto pool MLM Software in Surat

    ReplyDelete