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 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档