来自java应用程序错误的cmd命令

时间:2015-03-19 08:25:43

标签: java cmd

最近我添加了" adb devices"在nano ./bash_profile中,以便我可以从任何目录运行它。 我用了一个java应用程序来运行

 public static void main(String [] args) {
    executeCmd("adb devices");

}

private static void executeCmd(String string) {
    InputStream pipedOut = null;
    try {
        Process aProcess = Runtime.getRuntime().exec(string);

        // These two thread shall stop by themself when the process end
        Thread pipeThread = new Thread(new StreamGobber(aProcess.getInputStream()));
        Thread errorThread = new Thread(new StreamGobber(aProcess.getErrorStream()));

        pipeThread.start();
        errorThread.start();

        aProcess.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException ie) {
        ie.printStackTrace();
    }
}

class StreamGobber implements Runnable {

private InputStream Pipe;

public StreamGobber(InputStream pipe) {
    if(pipe == null) {
        throw new NullPointerException("bad pipe");
    }
    Pipe = pipe;
}

public void run() {
    try {
        byte buffer[] = new byte[2048];

        int read = Pipe.read(buffer);
        while(read >= 0) {
            System.out.write(buffer, 0, read);

            read = Pipe.read(buffer);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(Pipe != null) {
            try {
                Pipe.close();
            } catch (IOException e) {
            }
        }
    }

当我运行任何其他命令,例如" ls"它的工作正常!! 我正在使用mac ..

谢谢:)

1 个答案:

答案 0 :(得分:0)

可能是mac上的全局路径问题。您可以尝试使用绝对adb程序路径作为命令运行。