2 java1996 java1996 于 2017.01.11 09:10 提问

帮忙给这个java调用exe写一下注释,自己学看不懂这个代码

package spider.scrapyd;

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

public class Command {

public static String exec(String[] cmd, File path) {

    final StringBuffer stringBuffer = new StringBuffer();
    try {
        final Process process = Runtime.getRuntime().exec(cmd, null, path);
        new Thread(new Runnable() {
            public void run() {
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(process.getInputStream()));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        stringBuffer.append(line);
                    }
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        stringBuffer.append(line);
                    }
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        process.getOutputStream().close();
        process.waitFor();
        if (process.exitValue() == 0) {
            System.out.println("successfully execute command");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    String str = stringBuffer.toString();
    System.out.println(str);
    return str ;
}

public static void main(String[] args) {
//  String[] cmd = new String[] { "cmd", "/c", "wmic cpu get name" };
    String[] cmd = new String[] { "cmd", "/c", "scrapyd-deploy -p hrtencent -v r1.0.0 " };
    File f = new File("F:\\pywork\\PycharmProjects\\TencentHR\\hrtencent");
    String str = exec(cmd,f);
    System.out.println(str);
}

}

1个回答

tang_cheng
tang_cheng   2017.01.11 09:33
public static String exec(String[] cmd, File path) {

    final StringBuffer stringBuffer = new StringBuffer();
    try {
            // 执行cmd指定的命令,
        final Process process = Runtime.getRuntime().exec(cmd, null, path);
                // 启动监控线程,读取process的标准输出
        new Thread(new Runnable() {
            public void run() {
                try {
                                    // 创建reader对象,关联到process的输出
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(process.getInputStream()));
                    String line = null;
                                        // 读取输出内容,保存到stringBuffer中
                    while ((line = reader.readLine()) != null) {
                        stringBuffer.append(line);
                    }
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
                // 启动监控线程,读取process的错误输出(操作和标准输出一致)
        new Thread(new Runnable() {
            public void run() {
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        stringBuffer.append(line);
                    }
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        process.getOutputStream().close();
                // 等待process结束
        process.waitFor();

                // 如果process执行完后返回0,则打印成功信息
        if (process.exitValue() == 0) {
            System.out.println("successfully execute command");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

        // 打印获取的标准输出和错误输出,并返回
    String str = stringBuffer.toString();
    System.out.println(str);
    return str ;
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!