Java Runtimegetruntime Exec Example Journaldev

Best Java code snippets using java.lang.Runtime.exec (Showing top 20 results out of 16,065)

  • Add the Codota plugin to your IDE and get smart completions
                    

private void myMethod ()

{

}

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              Process openProcess(                                                              final                                                                                      String[] cmdAttribs)                                                                                    throws                                                                                      IOException {                                                                                                                                                                                                                                return                                                                                      Runtime.getRuntime().                                                              exec                                                              (cmdAttribs);                                                                                                  }                                                                                          
                                                                                                                                              public                                                                                                                                                  void                                                                                      mouseClicked(MouseEvent e) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    try                                                                                      {                                                                                                                                                                    Runtime.getRuntime().                                                              exec                                                              (                                                              "open http://localhost:8153/go"                                                              );                                                                                                                                                                    }                                                                                    catch                                                                                      (IOException e1) {                                                                                                                                                                                                                                                                                                                                                                          }                                                        }                                                                                          
                                                                                                                  String[] cmd = {                                                                                                  "/bin/sh"                                                              ,                                                                                                                                            "-c"                                                              ,                                                                                                                                            "ls /etc | grep release"                                                                                                                                                                };                                                                                                                                                          Process p = Runtime.getRuntime().                                                              exec                                                              (cmd);                                                                            
                                                                                                                                                                                                                            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                                                                                                                                                  boolean                                                                                      checkSudoPrivilege()                                                                                    throws                                                                                      InterruptedException, IOException {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            Process process = Runtime.getRuntime().                                                              exec                                                              (                                                              "sudo -v"                                                              );                                                                                                                                                                                                                                int                                                                                      exitCode = process.waitFor();                                                                                                                                                                                                                                return                                                                                      exitCode ==                                                                                    0                                                              ;                                                                                                                      }                                                        }                                  
                                                                                                                                              private                                                                                                                                                  static                                                                                      String getNvidiaStats()                                                                                    throws                                                                                      java.io.IOException {                                                                                                                                                                    String cmd =                                                                                    "nvidia-smi"                                                              ;                                                                                                                                                                    InputStream stdin = Runtime.getRuntime().                                                              exec                                                              (cmd).getInputStream();                                                                                                                                                                    InputStreamReader isr =                                                                                    new                                                                                      InputStreamReader(stdin);                                                                                                                                                                    BufferedReader br =                                                                                    new                                                                                      BufferedReader(isr);                                                                                                                                                                    StringBuilder sb =                                                                                    new                                                                                      StringBuilder();                                                                                                                      String s;                                                                                                                                                                                      while                                                                                      ((s = br.readLine()) != null) {                                                                                                                                                                    sb.append(s).append(                                                              "\n"                                                              );                                                                                                                      }                                                                                                                                                                                      return                                                                                      sb.toString();                                                                                                  }                                                                                          
                                                                                                                                              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);                                                                                                                      }                                                                            }                                                        }                                                                                          
                                                                                                                                              public                                                                                                                                                  static                                                                                      InputStream getBZip2PipedInputStream(String filename)                                                                                    throws                                                                                      IOException {                                                                                                                                                                    String bzcat = System.getProperty(                                                              "bzcat"                                                              ,                                                                                    "bzcat"                                                              );                                                                                                                      Runtime rt = Runtime.getRuntime();                                                                                                                          String cmd = bzcat +                                                                                    " "                                                                                      + filename;                                                                                                                                                                                                                                                                                                                                                                                                                        Process p = rt.                                                              exec                                                              (cmd);                                                                                                                                                                    Writer errWriter =                                                                                    new                                                                                      BufferedWriter(                                                              new                                                                                      OutputStreamWriter(System.err));                                                                                                                                                                    StreamGobbler errGobbler =                                                                                    new                                                                                      StreamGobbler(p.getErrorStream(), errWriter);                                                                                                                      errGobbler.start();                                                                                                                                                                                      return                                                                                      p.getInputStream();                                                                                                  }                                                                                          
                                                                                                                                              private                                                                                                                                                  void                                                                                      startPython(String tmpPath, String args)                                                                                    throws                                                                                      IOException {                                                                                                                      String pythonBinaryPath = config.getString(PythonOptions.PYTHON_BINARY_PATH);                                                                                                                                                                                                                                              try                                                                                      {                                                                                                                                                                    Runtime.getRuntime().                                                              exec                                                              (pythonBinaryPath);                                                                                                                                                                    }                                                                                    catch                                                                                      (IOException ignored) {                                                                                                                                                                                                                                throw                                                                                                                                                  new                                                                                      RuntimeException(pythonBinaryPath +                                                                                    " does not point to a valid python binary."                                                              );                                                                                                                      }                                                                                                                          process = Runtime.getRuntime().                                                              exec                                                              (pythonBinaryPath +                                                                                    " -B "                                                                                      + tmpPath + FLINK_PYTHON_PLAN_NAME + args);                                                                                                                                                                                                                                                                                        new                                                                                      Thread(                                                              new                                                                                      StreamPrinter(process.getInputStream())).start();                                                                                                                                                                                                                                new                                                                                      Thread(                                                              new                                                                                      StreamPrinter(process.getErrorStream())).start();                                                                                                                                                                                                                            server =                                                                                    new                                                                                      ServerSocket(                                                              0                                                              );                                                                                                                                                                    server.setSoTimeout(                                                              50                                                              );                                                                                                                                                                                                                            process.getOutputStream().write(                                                              "plan\n"                                                              .getBytes(ConfigConstants.DEFAULT_CHARSET));                                                                                                                      process.getOutputStream().flush();                                                        }                                                                                          
                                                                                                                                              public                                                                                                                                                  static                                                                                      String exec(File workingDirectory, String... commands) {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    Process process = Runtime.getRuntime().                                                              exec                                                              (commands, null, workingDirectory);                                                                                                                                                                                                                                return                                                                                      captureOutput(process).toString();                                                                                                                                                                    }                                                                                    catch                                                                                      (Exception e) {                                                                                                                                                                                                                                throw                                                                                      bomb(e);                                                                                                                      }                                                        }                                                                                          
                                                                                                                                              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                                                                                                                                                  void                                                                                      createCircularSymLink(                                                              final                                                                                      File file)                                                                                    throws                                                                                      IOException {                                                                                                                                                                                                                                if                                                                                      (!FilenameUtils.isSystemWindows()) {                                                                                                                      Runtime.getRuntime()                                                                                                                          .                                                              exec                                                              (                                                              "ln -s "                                                                                      + file +                                                                                    "/.. "                                                                                      + file +                                                                                    "/cycle"                                                              );                                                                                                                                                                    }                                                                                    else                                                                                      {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                      Runtime.getRuntime()                                                                                                                          .                                                              exec                                                              (                                                              "mklink /D "                                                                                      + file +                                                                                    "/cycle"                                                                                      + file +                                                                                    "/.. "                                                              );                                                                                                                                                                    }                                                                                    catch                                                                                      (final IOException ioe) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  }                                                                            }                                                        }                                                                                          
                                                                                                                                              private                                                                                                                                                  boolean                                                                                      commandExists( String command )                                                                                                  {                                                                                                                                                                                      try                                                                                                                                                                                    {                                                                                                                                                                                      return                                                                                      Runtime.getRuntime().                                                              exec                                                              ( command ).waitFor() ==                                                                                    0                                                              ;                                                                                                                      }                                                                                                                                                                                      catch                                                                                      ( Throwable e )                                                                                                                      {                                                                                                                                                                                      return                                                                                                                                                  false                                                              ;                                                                                                                      }                                                        }                                                        @Test                                  
                                                                                                                                              public                                                                                      OutputStream call()                                                                                    throws                                                                                      IOException {                                                                                                                                                                    Process p = Runtime.getRuntime().                                                              exec                                                              (cmd,                                                                                                                      Util.mapToEnv(inherit(envOverrides)),                                                                                                                          workDir == null ? null :                                                                                    new                                                                                      File(workDir));                                                                                                                                                                              List<String> cmdLines = Arrays.asList(cmd);                                                                                                                                                                                      new                                                                                      StreamCopyThread(                                                              "stdin copier for remote agent on "                                                              +cmdLines,                                                                                                                      p.getInputStream(), out.getOut()).start();                                                                                                                                                                                      new                                                                                      StreamCopyThread(                                                              "stderr copier for remote agent on "                                                              +cmdLines,                                                                                                                      p.getErrorStream(), err).start();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          return                                                                                                                                                  new                                                                                      RemoteOutputStream(p.getOutputStream());                                                                                                  }                                                                                          
                                                                                                                                              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                                                                                                                                                  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                                                                                                                                                  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();                                                                                                  }                                                                                          
                                                                                                                                              public                                                                                      File doThreadDump( Pair<Long, String> pid )                                                                                    throws                                                                                      Exception                                                                                                  {                                                                                                                          File outputFile =                                                                                    new                                                                                      File( outputDirectory, fileName(                                                                                    "threaddump"                                                              , pid ) );                                                                                                                                                                    log.info(                                                                                    "Creating thread dump of "                                                                                      + pid +                                                                                    " to "                                                                                      + outputFile.getAbsolutePath() );                                                                                                                                                                    String[] cmdarray =                                                                                    new                                                                                      String[] {                                                              "jstack"                                                              ,                                                                                    ""                                                                                      + pid.first()};                                                                                                                                                                    Process process = Runtime.getRuntime().                                                              exec                                                              ( cmdarray );                                                                                                                      writeProcessOutputToFile( process, outputFile );                                                                                                                                                                                      return                                                                                      outputFile;                                                                                                  }                                                                                          

wynnyoune1979.blogspot.com

Source: https://www.tabnine.com/code/java/methods/java.lang.Runtime/exec

0 Response to "Java Runtimegetruntime Exec Example Journaldev"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel