How to issue Windows command line commands from Java

Asked by rajendra.penumalli

Hi All,
I am using Sikuli (sikuli-java.jar) and I am trying to Automate Windows Application.
In this process i need to launch windows cmd line and i need to execute a command on that:

With the Following Code i am able to launch windows Command line :

App exeapp=App.open("cmd.exe");
exeapp.focus();
String cmd="tshark -i 4 -w dump.pcapng";

Now How to send a "cmd"command to Windows Command line?

Thanks in Advance,
Rajendra Prasad

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

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.

Can you help with this problem?

Provide an answer of your own, or ask rajendra.penumalli for more information if necessary.

To post a message you must log in.