Java Runtime Exec Waitfor Example

Best Java code snippets using java.lang.Process.waitFor (Showing top 20 results out of 18,486)

                                      @Override                                                                                                                                                                public                                                                                      Integer get()                                                                                    throws                                                                                      InterruptedException                                                                                                  {                                                                                                                                                                                      return                                                                                      process.                                                              waitFor                                                              ();                                                                                                  }                                                                                          
                                                                                                                                                                                                                            public                                                                                                                                                  boolean                                                                                      isOnline() {                                                                                                                                                                              Runtime runtime = Runtime.getRuntime();                                                                                                                                                                                      try                                                                                      {                                                                                                                                                                                                                            Process ipProcess = runtime.exec(                                                              "/system/bin/ping -c 1 8.8.8.8"                                                              );                                                                                                                                                                                                                                int                                                                                      exitValue = ipProcess.                                                              waitFor                                                              ();                                                                                                                                                                                                                                return                                                                                      (exitValue ==                                                                                    0                                                              );                                                                                                                                                                                                                            }                                                                                    catch                                                                                      (IOException e)          { e.printStackTrace(); }                                                                                                                                                                                                                                catch                                                                                      (InterruptedException e) { e.printStackTrace(); }                                                                                                                                                                                                                                                                                        return                                                                                                                                                  false                                                              ;                                                                                                  }                                  
                                                                                                                                              private                                                                                                                                                  int                                                                                      exec(String cmd)                                                                                    throws                                                                                      InterruptedException, IOException {                                                                                                                                                                    ProcessBuilder pb =                                                                                    new                                                                                      ProcessBuilder(exe, cmd);                                                                                                                                                                    pb.redirectErrorStream(                                                              true                                                              );                                                                                                                      Process p = pb.start();                                                                            p.getOutputStream().close();                                                                                                                          ByteArrayOutputStream baos =                                                                                    new                                                                                      ByteArrayOutputStream();                                                                                                                      copy(p.getInputStream(), baos);                                                                                                                                                                                      int                                                                                      r = p.                                                              waitFor                                                              ();                                                                                                                                                                                                                                if                                                                                      (r!=                                                              0                                                              )                                                                                                                                                                    LOGGER.info(exe+                                                              " cmd: output:\n"                                                              +baos);                                                                                                                                                                                                                                return                                                                                      r;                                                                                                  }                                                                                          
                                                          @Override                                                                                                                                                                                      public                                                                                                                                                  void                                                                                      close()                                                                                    throws                                                                                      IOException {                                                                                                                                                                                                                                if                                                                                      (process.isAlive()) {                                                                                                                      process.destroy();                                                                                                                                                                                      try                                                                                      {                                                                                                                                                                    process.                                                              waitFor                                                              (                                                              10                                                              , TimeUnit.SECONDS);                                                                                                                                                                    }                                                                                    catch                                                                                      (InterruptedException e) {                                                                                                                      Thread.currentThread().interrupt();                                                                            }                                                                            }                                                                            }                                                        }                                  
                                      @Override                                                                                                                                                                public                                                                                                                                                  void                                                                                      close()                                                                                    throws                                                                                      IOException {                                                                                                                      process.getOutputStream().close();                                                                                                                                                                                      try                                                                                      {                                                                                                                      errWriterThread.join();                                                                            outWriterThread.join();                                                                                                                          process.                                                              waitFor                                                              ();                                                                                                                                                                    }                                                                                    catch                                                                                      (InterruptedException e) {                                                                                                                                                                                                                                throw                                                                                                                                                  new                                                                                      ProcessException(e);                                                                                                                      }                                                        }                                                                                          
                                                                                                                                              private                                                                                                                                                  void                                                                                      generateHFile (FileDescriptor file)                                                                                    throws                                                                                      Exception {                                                                                                                      String className = getFullyQualifiedClassName(file);                                                                                                                          String command =                                                                                    "javah -classpath "                                                                                      + classpath +                                                                                    " -o "                                                                                      + jniDir.path() +                                                                                    "/"                                                                                      + className +                                                                                    ".h "                                                                                      + className;                                                                                                                      Process process = Runtime.getRuntime().exec(command);                                                                                                                          process.                                                              waitFor                                                              ();                                                                                                                                                                                                                                if                                                                                      (process.exitValue() !=                                                                                    0                                                              ) {                                                                                                                      System.out.println();                                                                                                                          System.out.println(                                                              "Command: "                                                                                      + command);                                                                                                                      InputStream errorStream = process.getErrorStream();                                                                                                                                                                                      int                                                                                      c =                                                                                    0                                                              ;                                                                                                                                                                                                                                while                                                                                      ((c = errorStream.read()) != -                                                              1                                                              ) {                                                                                                                                                                    System.out.print((                                                              char                                                              )c);                                                                                                                      }                                                                            }                                                        }                                                                                          
                                                                                                                                              private                                                                                                                                                  void                                                                                      generateHFile (FileDescriptor file)                                                                                    throws                                                                                      Exception {                                                                                                                      String className = getFullyQualifiedClassName(file);                                                                                                                          String command =                                                                                    "javah -classpath "                                                                                      + classpath +                                                                                    " -o "                                                                                      + jniDir.path() +                                                                                    "/"                                                                                      + className +                                                                                    ".h "                                                                                      + className;                                                                                                                      Process process = Runtime.getRuntime().exec(command);                                                                                                                          process.                                                              waitFor                                                              ();                                                                                                                                                                                                                                if                                                                                      (process.exitValue() !=                                                                                    0                                                              ) {                                                                                                                      System.out.println();                                                                                                                          System.out.println(                                                              "Command: "                                                                                      + command);                                                                                                                      InputStream errorStream = process.getErrorStream();                                                                                                                                                                                      int                                                                                      c =                                                                                    0                                                              ;                                                                                                                                                                                                                                while                                                                                      ((c = errorStream.read()) != -                                                              1                                                              ) {                                                                                                                                                                    System.out.print((                                                              char                                                              )c);                                                                                                                      }                                                                            }                                                        }                                                                                          
                                                                                                                                              private                                                                                                                                                  static                                                                                      StringBuilder captureOutput(Process process)                                                                                    throws                                                                                      IOException, InterruptedException {                                                                                                                                                                    BufferedReader output =                                                                                    new                                                                                      BufferedReader(                                                              new                                                                                      InputStreamReader(process.getInputStream()));                                                                                                                                                                    BufferedReader error =                                                                                    new                                                                                      BufferedReader(                                                              new                                                                                      InputStreamReader(process.getErrorStream()));                                                                                                                                                                    StringBuilder result =                                                                                    new                                                                                      StringBuilder();                                                                                                                                                                    result.append(                                                              "output:\n"                                                              );                                                                                                                      dump(output, result);                                                                                                                          result.append(                                                              "error:\n"                                                              );                                                                                                                      dump(error, result);                                                                                                                          process.                                                              waitFor                                                              ();                                                                                                                                                                                                                                return                                                                                      result;                                                                                                  }                                                                                          
                                                                                                                                                                    private                                                                                                                                                  static                                                                                                                                                  void                                                                                      clearScreen()                                                                                    throws                                                                                      Exception                                                                                                                      {                                                                                                                                                                                      if                                                                                      (SystemUtil.osName().contains(                                                              "win"                                                              ))                                                                                                                      {                                                                                                                                                                                      new                                                                                      ProcessBuilder(                                                              "cmd"                                                              ,                                                                                    "/c"                                                              ,                                                                                    "cls"                                                              ).inheritIO().start().                                                              waitFor                                                              ();                                                                                                                      }                                                                                                                                                                                      else                                                                                                                                                                                    {                                                                            System.out.print(ANSI_CLS + ANSI_HOME);                                                                            }                                                                            }                                                        }                                  
                                                                                                                                              public                                                                                                                                                  static                                                                                                                                                  void                                                                                      callShell(                                                              final                                                                                      String shellString,                                                                                    final                                                                                      InternalLogger log) {                                                                                                                      Process process = null;                                                                                                                                                                                      try                                                                                      {                                                                                                                      String[] cmdArray = splitShellString(shellString);                                                                            process = Runtime.getRuntime().exec(cmdArray);                                                                                                                          process.                                                              waitFor                                                              ();                                                                                                                                                                    log.info(                                                              "CallShell: <{}> OK"                                                              , shellString);                                                                                                                                                                    }                                                                                    catch                                                                                      (Throwable e) {                                                                                                                                                                    log.error(                                                              "CallShell: readLine IOException, {}"                                                              , shellString, e);                                                                                                                                                                    }                                                                                    finally                                                                                      {                                                                                                                                                                                                                                if                                                                                      (null != process)                                                                                                                      process.destroy();                                                                            }                                                        }                                                                                          
                                                                                                                                              private                                                                                                                                                  boolean                                                                                      commandExists( String command )                                                                                                  {                                                                                                                                                                                      try                                                                                                                                                                                    {                                                                                                                                                                                      return                                                                                      Runtime.getRuntime().exec( command ).                                                              waitFor                                                              () ==                                                                                    0                                                              ;                                                                                                                      }                                                                                                                                                                                      catch                                                                                      ( Throwable e )                                                                                                                      {                                                                                                                                                                                      return                                                                                                                                                  false                                                              ;                                                                                                                      }                                                        }                                                        @Test                                  
                                                                                                                                              private                                                                                                                                                  void                                                                                      setupSymlink(                                                              final                                                                                      File res,                                                                                    final                                                                                      File link)                                                                                    throws                                                                                      Exception {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    final                                                                                      List<String> args =                                                                                    new                                                                                      ArrayList<>();                                                                                                                                                                    args.add(                                                              "ln"                                                              );                                                                                                                                                                    args.add(                                                              "-s"                                                              );                                                                                                                                                                              args.add(res.getAbsolutePath());                                                                            args.add(link.getAbsolutePath());                                                                                                                                    Process proc;                                                                                                                                                                                  proc = Runtime.getRuntime().exec(args.toArray(                                                              new                                                                                      String[args.size()]));                                                                                                                                                                    proc.                                                              waitFor                                                              ();                                                                                                  }                                                                                          
                                                                                                                                              private                                                                                                                                                  static                                                                                                                                                  void                                                                                      stopAlluxioFramework()                                                                                    throws                                                                                      Exception {                                                                                                                      String stopScript = PathUtils.concatPath(sConf.get(PropertyKey.HOME),                                                                                                                                                                                      "integration"                                                              ,                                                                                    "mesos"                                                              ,                                                                                    "bin"                                                              ,                                                                                    "alluxio-mesos-stop.sh"                                                              );                                                                                                                                                                    ProcessBuilder pb =                                                                                    new                                                                                      ProcessBuilder(stopScript);                                                                                                                                                                    pb.start().                                                              waitFor                                                              ();                                                                                                                                                                                                                                                                                                                                                                                                                        CommonUtils.sleepMs(                                                              5000                                                              );                                                                                                  }                                                                                          
                                                                                                                                              private                                                                                                                                                  void                                                                                      writeProcessOutputToFile( Process process, File outputFile )                                                                                    throws                                                                                      Exception                                                                                                  {                                                                                                                          BufferedReader reader =                                                                                    new                                                                                      BufferedReader(                                                                                    new                                                                                      InputStreamReader( process.getInputStream() ) );                                                                                                                      String line = null;                                                                                                                                                                                      try                                                                                      ( PrintStream out =                                                                                    new                                                                                      PrintStream( outputFile ) )                                                                                                                      {                                                                                                                                                                                      while                                                                                      ( (line = reader.readLine()) != null )                                                                                                                      {                                                                            out.println( line );                                                                            }                                                                            }                                                                                                                          process.                                                              waitFor                                                              ();                                                                                                  }                                                                                          
                                                                                                                                              private                                                                                      Process mockProcess(                                                              final                                                                                      InputStream outputStream,                                                                                    final                                                                                      InputStream errorStream,                                                                                    final                                                                                      OutputStream inputStream)                                                                                    throws                                                                                      InterruptedException {                                                                                                                                                                                                                                final                                                                                      Process subProcess = mock(Process.                                                              class                                                              );                                                                                                                                                                    when(subProcess.                                                              waitFor                                                              ()).thenReturn(                                                              42                                                              );                                                                                                                      when(subProcess.getInputStream()).thenReturn(outputStream);                                                                            when(subProcess.getErrorStream()).thenReturn(errorStream);                                                                            when(subProcess.getOutputStream()).thenReturn(inputStream);                                                                                                                                                                                      return                                                                                      subProcess;                                                                                                  }                                                                                          
                                                                                                                                              private                                                                                                                                                  static                                                                                                                                                  void                                                                                      kill(                                                                                    int                                                                                      signal, Process process )                                                                                    throws                                                                                      Exception                                                                                                  {                                                                                                                                                                                      int                                                                                      exitCode =                                                                                    new                                                                                      ProcessBuilder(                                                                                    "kill"                                                              ,                                                                                    "-"                                                                                      + signal, pidOf( process ) ).start().                                                              waitFor                                                              ();                                                                                                                                                                                                                                if                                                                                      ( exitCode !=                                                                                    0                                                                                      )                                                                                                                      {                                                                                                                                                                                      throw                                                                                                                                                  new                                                                                      IllegalStateException(                                                                                    "<kill -"                                                                                      + signal +                                                                                    "> failed, exit code: "                                                                                      + exitCode );                                                                                                                      }                                                        }                                                                                          
                                                                                                                                              private                                                                                                                                                  boolean                                                                                      chmod(                                                              final                                                                                      File file,                                                                                    final                                                                                                                                                  int                                                                                      mode,                                                                                    final                                                                                                                                                  boolean                                                                                      recurse)                                                                                                                                                                                                                                throws                                                                                      InterruptedException {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    final                                                                                      List<String> args =                                                                                    new                                                                                      ArrayList<>();                                                                                                                                                                    args.add(                                                              "chmod"                                                              );                                                                                                                                                                                                                                                                                        if                                                                                      (recurse) {                                                                                                                                                                    args.add(                                                              "-R"                                                              );                                                                                                                      }                                                                                                                                    args.add(Integer.toString(mode));                                                                            args.add(file.getAbsolutePath());                                                                                                                                    Process proc;                                                                                                                                                                                                                                              try                                                                                      {                                                                                                                      proc = Runtime.getRuntime().exec(                                                                                                                          args.toArray(                                                              new                                                                                      String[args.size()]));                                                                                                                                                                    }                                                                                    catch                                                                                      (final IOException e) {                                                                                                                                                                                                                                return                                                                                                                                                  false                                                              ;                                                                                                                      }                                                                                                                                                                                      final                                                                                                                                                  int                                                                                      result = proc.                                                              waitFor                                                              ();                                                                                                                                                                                                                                return                                                                                      result ==                                                                                    0                                                              ;                                                                                                  }                                                                                          
                                                                                                                                              private                                                                                                                                                  void                                                                                      killApplicationMaster(                                                              final                                                                                      String processName)                                                                                    throws                                                                                      IOException, InterruptedException {                                                                                                                                                                                                                                final                                                                                      Process exec = Runtime.getRuntime().exec(                                                              "pkill -f "                                                                                      + processName);                                                                                                                                                                    assertThat(exec.                                                              waitFor                                                              (), is(                                                              0                                                              ));                                                                                                  }                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            public                                                                                                                                                  static                                                                                      ProcessResult run(Process process)                                                                                    throws                                                                                      IOException, InterruptedException {                                                                                                                                                                    ByteArrayOutputStream baos =                                                                                    new                                                                                      ByteArrayOutputStream();                                                                                                                                                                                                                            StreamGobbler outputGobbler =                                                                                    new                                                                                      StreamGobbler(process.getInputStream(), baos,                                                                                    "out>"                                                              );                                                                                                                                                                    StreamGobbler errorGobbler =                                                                                    new                                                                                      StreamGobbler(process.getErrorStream(), baos,                                                                                    "err>"                                                              );                                                                                                                                                                              outputGobbler.start();                                                                            errorGobbler.start();                                                                                                                                                                                                                                              int                                                                                      result = process.                                                              waitFor                                                              ();                                                                                                                                                                              outputGobbler.waitFor();                                                                            errorGobbler.waitFor();                                                                                                                                                                                                                                              return                                                                                                                                                  new                                                                                      ProcessResult(result, baos.toString());                                                                                                  }                                                                                          
                                                                                                                                              public                                                                                                                                                  void                                                                                      doHeapDump( Pair<Long, String> pid )                                                                                    throws                                                                                      Exception                                                                                                  {                                                                                                                          File outputFile =                                                                                    new                                                                                      File( outputDirectory, fileName(                                                                                    "heapdump"                                                              , pid ) );                                                                                                                                                                    log.info(                                                                                    "Creating heap dump of "                                                                                      + pid +                                                                                    " to "                                                                                      + outputFile.getAbsolutePath() );                                                                                                                                                                    String[] cmdarray =                                                                                    new                                                                                      String[] {                                                              "jmap"                                                              ,                                                                                    "-dump:file="                                                                                      + outputFile.getAbsolutePath(),                                                                                    ""                                                                                      + pid.first() };                                                                                                                                                                    Runtime.getRuntime().exec( cmdarray ).                                                              waitFor                                                              ();                                                                                                  }                                                                                          

densmorebleenter.blogspot.com

Source: https://www.tabnine.com/code/java/methods/java.lang.Process/waitFor

0 Response to "Java Runtime Exec Waitfor Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel