Process Class in Java Example

Best Java code snippets using java.lang.Process (Showing top 20 results out of 24,813)

                                                                                                                                              private                                                                                                                                                  static                                                                                                                                                  void                                                                                      closeStreams(@Nullable Process process) {                                                                                                                                                                                                                                if                                                                                      (process != null) {                                                                                                                                                                    IOUtils.closeQuietly(process.                                                              getInputStream                                                              ());                                                                                                                                                                    IOUtils.closeQuietly(process.                                                              getOutputStream                                                              ());                                                                                                                                                                    IOUtils.closeQuietly(process.                                                              getErrorStream                                                              ());                                                                                                                      }                                                        }                                                                                          
                                      @Override                                                                                                                                                                public                                                                                      Integer get()                                                                                    throws                                                                                      InterruptedException                                                                                                  {                                                                                                                                                                                      return                                                                                      process.                                                              waitFor                                                              ();                                                                                                  }                                                                                          
                                                                                                                                              public                                                                                                                                                  void                                                                                      kill() {                                                                                                                                                                    _process.                                                              destroy                                                              ();                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    _process.                                                              waitFor                                                              ();                                                                                                                                                                    }                                                                                    catch                                                              ( InterruptedException xe ) {                                                                                                                                                                                                                                                                                                                                                                          }                                                        }                                                                                          
                                                          @Override                                                                                                                                                                                      public                                                                                                                                                  void                                                                                      close()                                                                                    throws                                                                                      IOException {                                                                                                                                                                                                                                if                                                                                      (process.                                                              isAlive                                                              ()) {                                                                                                                                                                    process.                                                              destroy                                                              ();                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    process.                                                              waitFor                                                              (                                                              10                                                              , TimeUnit.SECONDS);                                                                                                                                                                    }                                                                                    catch                                                                                      (InterruptedException e) {                                                                                                                      Thread.currentThread().interrupt();                                                                            }                                                                            }                                                                            }                                                        }                                  
                                                                                                                                              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;                                                                                                  }                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                            Process proc = Runtime.getRuntime().exec(                                                              "java -jar A.jar"                                                              );                                                                                                                                                                                                                                                                                                            InputStream in = proc.                                                              getInputStream                                                              ();                                                                                                                                            InputStream err = proc.                                                              getErrorStream                                                              ();                                                                            
                                                                                                                                              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;                                                                                                  }                                                                                          
                                                                                                                                              public                                                                                                                                                  static                                                                                                                                                  int                                                                                      processLauncherAndWait(Map<String, Object> conf, String user, List<String> args,                                                                                                                                                                                                                                final                                                                                      Map<String, String> environment,                                                                                    final                                                                                      String logPreFix)                                                                                                                                                                                                                                throws                                                                                      IOException {                                                                                                                                                                                                                                int                                                                                      ret =                                                                                    0                                                              ;                                                                                                                      Process process = processLauncher(conf, user, null, args, environment, logPreFix, null, null);                                                                                                                                                                                      if                                                                                      (StringUtils.isNotBlank(logPreFix)) {                                                                                                                                                                    Utils.readAndLogStream(logPreFix, process.                                                              getInputStream                                                              ());                                                                                                                      }                                                                                                                                                                                      try                                                                                      {                                                                                                                                                                    process.                                                              waitFor                                                              ();                                                                                                                                                                    }                                                                                    catch                                                                                      (InterruptedException e) {                                                                                                                                                                    LOG.info(                                                              "{} interrupted."                                                              , logPreFix);                                                                                                                      }                                                                                                                          ret = process.                                                              exitValue                                                              ();                                                                                                                                                                                                                                return                                                                                      ret;                                                                                                  }                                                                                          
                                                                                                                                              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;                                                                                                  }                                                                                          
                                                                                                                                              public                                                                                                                                                  static                                                                                                                                                  void                                                                                      exec(ArrayList<String> args, File path)                                                                                    throws                                                                                      RuntimeException, IOException, InterruptedException {                                                                                                                                                                    ProcessBuilder ps =                                                                                    new                                                                                      ProcessBuilder(args);                                                                                                                                                                    ps.redirectErrorStream(                                                              true                                                              );                                                                                                                                                                                                                                if                                                                                      (path != null) {                                                                                                                      ps.directory(path);                                                                            }                                                                            Process pr = ps.start();                                                                            BufferedReader ins = null;                                                                                                                                                                                      try                                                                                      {                                                                                                                                                                    ins =                                                                                    new                                                                                      BufferedReader(                                                              new                                                                                      InputStreamReader(pr.                                                              getInputStream                                                              ()));                                                                                                                      String line;                                                                                                                                                                                      while                                                                                      ((line = ins.readLine()) != null) {                                                                                                                      System.out.println(line);                                                                            }                                                                                                                                                                                      if                                                                                      (pr.                                                              waitFor                                                              () !=                                                                                    0                                                              ) {                                                                                                                                                                                                                                throw                                                                                                                                                  new                                                                                      RuntimeException(                                                              "exec cmd failed! args: "                                                                                      + args);                                                                                                                      }                                                                                                                          }                                                                                    finally                                                                                      {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    pr.                                                              destroy                                                              ();                                                                                                                                                                    }                                                                                    catch                                                                                      (Throwable ignored) {                                                                                                                                                                                                                                                                                                                                                                          }                                                                            StreamUtil.closeQuietly(ins);                                                                            }                                                        }                                                                                          
                                      @Override                                                                                                                                                                public                                                                                                                                                  void                                                                                      run () {                                                                                                                                                                    BufferedReader reader =                                                                                    new                                                                                      BufferedReader(                                                              new                                                                                      InputStreamReader(process.                                                              getInputStream                                                              ()));                                                                                                                      String line = null;                                                                                                                                                                                      try                                                                                      {                                                                                                                                                                                                                                while                                                                                      ((line = reader.readLine()) != null) {                                                                                                                                                                                                                                                                                                                                                                          printFileLineNumber(line);                                                                            }                                                                                                                          }                                                                                    catch                                                                                      (IOException e) {                                                                                                                      e.printStackTrace();                                                                            }                                                        }                                                                                          
                                                                                                                                              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                                                                                      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                                                              ();                                                                                                  }                                                                                          
                                                                                                                                              public                                                                                                                                                  void                                                                                      close() {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    process.                                                              exitValue                                                              ();                                                                                                                                                                    }                                                                                    catch                                                                                      (NullPointerException ignored) {                                                                                                                                                                                                                                                                            }                                                                                    catch                                                                                      (IllegalThreadStateException ignored) {                                                                                                                                                                                                                                                                            process.                                                              destroy                                                              ();                                                                                                                                                                    }                                                                                    finally                                                                                      {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                                                                                if                                                                                      (server != null) {                                                                                                                      server.close();                                                                            }                                                                                                                          }                                                                                    catch                                                                                      (IOException e) {                                                                                                                                                                    LOG.error(                                                              "Failed to close socket."                                                              , e);                                                                                                                      }                                                                            }                                                        }                                                                                          
                                                                                                                                                                    public                                                                                                                                                  int                                                                                      run()                                                                                    throws                                                                                      IOException, InterruptedException {                                                                                                                                                                    File workingDir = builder.directory() == null ?                                                                                    new                                                                                      File(                                                              "."                                                              ) : builder.directory();                                                                                                                                                                    System.out.println(String.format(                                                              "Trying to run command: %s from %s"                                                              , Arrays.toString(builder.command().toArray()), workingDir.getAbsolutePath()));                                                                                                                      Process process = builder.start();                                                                                                                                                                                      int                                                                                      exitCode = process.                                                              waitFor                                                              ();                                                                                                                                                                    System.out.println(                                                              "Finished command: "                                                                                      + Arrays.toString(builder.command().toArray()) +                                                                                    ". Exit code: "                                                                                      + exitCode);                                                                                                                                                                                                                                if                                                                                      (exitCode !=                                                                                    0                                                              ) {                                                                                                                                                                                                                                if                                                                                      (failOnError) {                                                                                                                                                                                                                                throw                                                                                                                                                  new                                                                                      RuntimeException(String.format(                                                              "Command exited with code %s. \n Exception: %s"                                                              , exitCode, IOUtils.toString(process.                                                              getErrorStream                                                              ())));                                                                                                                                                                    }                                                                                    else                                                                                      {                                                                                                                                                                    LOGGER.error(                                                              "Command exited with code {}. \n Exception: {}"                                                              , exitCode, IOUtils.toString(process.                                                              getErrorStream                                                              ()));                                                                                                                      }                                                                            }                                                                                                                                                                                      return                                                                                      exitCode;                                                                                                                      }                                                        }                                  
                                                                                                                                                                    private                                                                                                                                                  static                                                                                                                                                  boolean                                                                                      startProcess (String[] commands, File directory,                                                                                    final                                                                                      CharCallback callback) {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                                                                                final                                                                                      Process process =                                                                                    new                                                                                      ProcessBuilder(commands).redirectErrorStream(                                                              true                                                              ).directory(directory).start();                                                                                                                                                                                                                            Thread t =                                                                                    new                                                                                      Thread(                                                              new                                                                                      Runnable() {                                                                                                                      @Override                                                                                                                                                                                      public                                                                                                                                                  void                                                                                      run () {                                                                                                                                                                    BufferedReader reader =                                                                                    new                                                                                      BufferedReader(                                                              new                                                                                      InputStreamReader(process.getInputStream()),                                                                                    1                                                              );                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                                                                                int                                                                                      c =                                                                                    0                                                              ;                                                                                                                                                                                                                                while                                                                                      ((c = reader.read()) != -                                                              1                                                              ) {                                                                                                                                                                    callback.character((                                                              char                                                              )c);                                                                                                                      }                                                                                                                          }                                                                                    catch                                                                                      (IOException e) {                                                                                                                                                                                                                                                                                      }                                                                            }                                                                            });                                                                                                                          t.setDaemon(                                                              true                                                              );                                                                                                                      t.start();                                                                                                                          process.                                                              waitFor                                                              ();                                                                                                                      t.interrupt();                                                                                                                                                                                      return                                                                                      process.                                                              exitValue                                                              () ==                                                                                    0                                                              ;                                                                                                                                                                    }                                                                                    catch                                                                                      (Exception e) {                                                                                                                      e.printStackTrace();                                                                                                                                                                                      return                                                                                                                                                  false                                                              ;                                                                                                                      }                                                                            }                                                        }                                  
                                                                                                                                        Process process =                                                                                    new                                                                                      ProcessBuilder(                                                              "sysctl"                                                              , sysctlKey).start();                                                                                                                                                                                                          try                                                                                      {                                                                                                                                                                    InputStream is = process.                                                              getInputStream                                                              ();                                                                                                                                                                    InputStreamReader isr =                                                                                    new                                                                                      InputStreamReader(is);                                                                                                                                                                    BufferedReader br =                                                                                    new                                                                                      BufferedReader(isr);                                                                                                                                                                                                                            process.                                                              destroy                                                              ();                                                                                                                                    
                                      @Override                                                                                                                                                                public                                                                                                                                                  void                                                                                      close()                                                                                    throws                                                                                      IOException {                                                                                                                                                                    process.                                                              getOutputStream                                                              ().close();                                                                                                                                                                                                                                try                                                                                      {                                                                                                                      errWriterThread.join();                                                                            outWriterThread.join();                                                                                                                          process.                                                              waitFor                                                              ();                                                                                                                                                                    }                                                                                    catch                                                                                      (InterruptedException e) {                                                                                                                                                                                                                                throw                                                                                                                                                  new                                                                                      ProcessException(e);                                                                                                                      }                                                        }                                                                                          

millersentur.blogspot.com

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

0 Response to "Process Class in Java Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel