java1996 2017-01-11 01:10 采纳率: 0%
浏览 851

帮忙给这个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 2017-01-11 01: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 ;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形