Java Shell Command sendCommand(String path, String cmd)

Here you can find the source of sendCommand(String path, String cmd)

Description

send Command

License

Apache License

Declaration

public synchronized static void sendCommand(String path, String cmd) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

public class Main {
    public synchronized static void sendCommand(String path, String cmd) {

        new Thread(() -> {
            Runtime run = Runtime.getRuntime();

            try {
                Process p = run.exec(cmd, null, new File(path));
                BufferedInputStream in = new BufferedInputStream(p.getErrorStream());
                BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
                String lineStr;/* www. j ava2 s  .co m*/
                while ((lineStr = inBr.readLine()) != null) {
                    System.out.println(lineStr);
                }

                p.waitFor();
                inBr.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
}

Related

  1. runShell(String shStr)
  2. runShellCommand(String command)
  3. runShellCommand(String command)
  4. runShellCommand(String command)
  5. runShellCommand(String[] cmd, StringBuilder outputLines, StringBuilder errorLines)