public static void proc(List<String> list) {
    Process p = null;
    // ProcessBuilder pb = new ProcessBuilder("java", "-jar", "swordp.jar",
    int i = list.size();
    while (true) {
      long begin = System.currentTimeMillis();
      // "图书>计算机与互联网>网络与通讯>");
      if (i < 1) {
        i = list.size();
        // Runtime.getRuntime().exit(0);
      }
      System.out.println("path: " + list.get(i - 1));
      ProcessBuilder pb = new ProcessBuilder("java", "-jar", "swordp.jar", list.get(i - 1));
      // use java lib/ext C:\Java\jdk1.7.0_15\jre\lib\ext
      pb.directory(new File("d://book"));
      try {
        p = pb.start();
      } catch (Exception ex) {
        ex.printStackTrace();
      }

      System.out.println(p.toString() + "time: " + DateTimeUtil.getCurrentDateTime());
      while (System.currentTimeMillis() - begin < 1000 * 60 * 5) {
        // Thread
        try {
          Thread.sleep(1000 * 60 * 2);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      p.destroy();
      i--;
      System.out.println("end" + DateTimeUtil.getCurrentDateTime());
    }
  }
  public static int executeShell(String shellCommand) throws IOException {
    int success = 0;
    StringBuffer stringBuffer = new StringBuffer();
    BufferedReader bufferedReader = null;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");
    try {
      stringBuffer
          .append(dateFormat.format(new Date()))
          .append("Read for Shell command ")
          .append(shellCommand)
          .append(" \r\n");
      Process pid = null;
      String[] cmd = {"/bin/sh", "-c", shellCommand};
      pid = Runtime.getRuntime().exec(cmd);
      if (pid != null) {
        stringBuffer.append("Process ID: ").append(pid.toString()).append("\r\n");
        bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);
        pid.waitFor();
      } else {
        stringBuffer.append("No PID.\r\n");
      }
      stringBuffer.append(dateFormat.format(new Date())).append("Result of Shell execution: \r\n");
      String line = null;
      while (bufferedReader != null && (line = bufferedReader.readLine()) != null) {
        stringBuffer.append(line).append("\r\n");
      }

      BufferedReader stdError = new BufferedReader(new InputStreamReader(pid.getErrorStream()));
      while (stdError != null && (line = stdError.readLine()) != null) {
        stringBuffer.append(line).append("\r\n");
      }

    } catch (Exception ioe) {
      stringBuffer
          .append("Exception from Shell execution: \r\n")
          .append(ioe.getMessage())
          .append("\r\n");
    } finally {
      if (bufferedReader != null) {
        OutputStreamWriter outputStreamWriter = null;
        try {
          bufferedReader.close();
          OutputStream outputStream =
              new FileOutputStream(
                  Constants.SHELL_DIRECTORY + File.separator + Constants.SHELL_EXECUTION_LOG);
          outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
          outputStreamWriter.write(stringBuffer.toString());
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          outputStreamWriter.close();
        }
      }
      success = 1;
    }
    return success;
  }
 @Override
 public boolean execute() {
   ProcessBuilder install_apk = new ProcessBuilder(mArgs);
   try {
     Process p = install_apk.start();
     System.out.println("Install.execute() :" + p.toString());
     return translateOutput(p);
   } catch (IOException e) {
     e.printStackTrace();
     return false;
   }
 }
Exemple #4
0
 public int executeShell(String shellCommand) throws IOException {
   int success = 0;
   StringBuffer stringBuffer = new StringBuffer();
   BufferedReader bufferedReader = null;
   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");
   try {
     stringBuffer
         .append(dateFormat.format(new Date()))
         .append("准备执行Shell命令 ")
         .append(shellCommand)
         .append(" \r\n");
     Process pid = null;
     String[] cmd = {"/bin/sh", "-c", shellCommand};
     // 执行Shell命令
     pid = Runtime.getRuntime().exec(cmd);
     if (pid != null) {
       stringBuffer.append("进程号:").append(pid.toString()).append("\r\n");
       // bufferedReader用于读取Shell的输出内容
       bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);
       pid.waitFor();
     } else {
       stringBuffer.append("没有pid\r\n");
       stringBuffer.append(dateFormat.format(new Date())).append("Shell命令执行完毕\r\n执行结果为:\r\n");
       String line = null;
       // 读取Shell的输出内容,并添加到stringBuffer中
       while (bufferedReader != null && (line = bufferedReader.readLine()) != null) {
         stringBuffer.append(line).append("\r\n");
       }
     }
   } catch (Exception ioe) {
     stringBuffer.append("执行Shell命令时发生异常:\r\n").append(ioe.getMessage()).append("\r\n");
   } finally {
     if (bufferedReader != null) {
       OutputStreamWriter outputStreamWriter = null;
       try {
         bufferedReader.close();
         // 将Shell的执行情况输出到日志文件中
         OutputStream outputStream = new FileOutputStream(executeShellLogFile);
         outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
         outputStreamWriter.write(stringBuffer.toString());
       } catch (Exception e) {
         e.printStackTrace();
       } finally {
         outputStreamWriter.close();
       }
       success = 1;
     }
   }
   return success;
 }
 /** Kills all the launched applications */
 public void killAllProcesses() {
   for (Process p : m_listOfProcesses) {
     if (HermesConfig.DEBUG) {
       Logger.getLogger(DeploymentDaemon.class.getName())
           .log(
               Level.INFO,
               "DeploymentDaemon: killAllProcesses(): killing process {0}",
               p.toString());
     }
     p.destroy();
   }
   m_listOfProcesses.clear();
   if (HermesConfig.DEBUG) {
     Logger.getLogger(DeploymentDaemon.class.getName())
         .log(Level.INFO, "DeploymentDaemon: killAllProcesses(): ended");
   }
 }
 /** Destroy */
 protected void destroy() {
   String str = mProcess.toString();
   try {
     int i = str.indexOf("=") + 1;
     int j = str.indexOf("]");
     str = str.substring(i, j);
     int pid = Integer.parseInt(str);
     try {
       android.os.Process.killProcess(pid);
     } catch (Exception e) {
       try {
         mProcess.destroy();
       } catch (Exception ex) {
         ex.printStackTrace();
       }
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  public ProcessDataType(Process process, String... fields) {
    super(process.getTypeId(), process.toString(), fields);

    this.process = process;
  }
  static void downloadDir(String dir) {
    try {
      String localDir = "/media/sdd/corpus/";

      (new File(localDir + dir)).mkdirs();

      String line;

      URL url =
          new URL(
              "http://s3.amazonaws.com/aws-publicdatasets/trec/kba/kba-streamcorpus-2013-v0_2_0-english-and-unknown-language/"
                  + dir
                  + "/index.html");
      InputStream is = url.openStream(); // throws an IOException
      BufferedReader br2 = new BufferedReader(new InputStreamReader(is));

      Pattern p = Pattern.compile("a href=\"([^\"]+)\"");
      while ((line = br2.readLine()) != null) {
        Matcher m = p.matcher(line);
        if (m.find()) {
          String linkStr = m.group(1);
          if (linkStr.contains("gpg")) {
            System.out.println(linkStr);
            String gpgStr =
                "http://s3.amazonaws.com/aws-publicdatasets/trec/kba/kba-streamcorpus-2013-v0_2_0-english-and-unknown-language/"
                    + dir
                    + "/"
                    + linkStr;

            String command =
                "wget -O - "
                    + gpgStr
                    + " |   gpg --no-permission-warning --trust-model always --output "
                    + localDir
                    + dir
                    + "/"
                    + linkStr.substring(0, linkStr.length() - 4);

            String[] cmd = {"/bin/sh", "-c", command};

            System.out.println(command);
            Process process = Runtime.getRuntime().exec(cmd);
            // System.out.println(process.exitValue());
            System.out.println(process.toString());
            BufferedReader stdOut =
                new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader stdErr =
                new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = stdOut.readLine()) != null) {
              System.out.println(line);
            }

            System.out.println("///////////////////////////////");
            while ((line = stdErr.readLine()) != null) {
              System.out.println(line);
            }
            process.destroy();
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }