首页 > 代码库 > Java 调用外部命令

Java 调用外部命令

Process process = Runtime.getRuntime().exec("c:\\test.bat"); // 执行一个系统命令
    InputStream fis = process.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String line = null;
    StringBuffer cmdout = new StringBuffer();
    while ((line = br.readLine()) != null) {
        cmdout.append(line).append(System.getProperty("line.separator"));
    }
    br.close();
    process.destroy();
    System.out.print(cmdout.toString().trim());
    int result = 0;
    if (cmdout.toString().contains("BUILD SUCCESSFUL")) {
        result = 1;
        System.out.println("\n" + "result =" + result);
    }
    System.out.print("----" + System.getProperty("line.separator"));

Java 调用外部命令