← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #264504]: How to issue Windows command line commands from Java

 

Question #264504 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/264504

    Status: Open => Answered

RaiMan proposed the following answer:
a bit nervous and demanding.

issuing the same question multiple times could be misinterpreted as spam.
So be a bit patient.

first of course I recommend to upgrade to version 1.1.0
(http://sikulix.com)

Using a command line window to issue commands using Sikuli features is
overkill, especially from Java.

The easiest way is to use the Java RunTime.
Here the relevant part of the implementation in version 1.1.0 
(result = RunTime.get().runCmd("cmd.exe /C  tshark -i 4 -w dump.pcapng"))

      Process process = Runtime.getRuntime().exec(args);
      BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
      BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
      String s;
      while ((s = stdInput.readLine()) != null) {
        if (!s.isEmpty()) {
          result += s + NL;
        }
      }
      if ((s = stdError.readLine()) != null) {
        hasError = true;
        if (!s.isEmpty()) {
          error += s + NL;
        }
      }
      process.waitFor();
      retVal = process.exitValue();
      process.destroy();

now you might evaluate the result lines returned in result and/or error.

depending on the commands you use (e.g. start) you might start other processes to run in parallel.
Of course there are many more powerful means (e.g.ProcessBuilder) to accomplish such things.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.