Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Red Hat, Inc. and/or its affiliates.
   *
   * Licensed under the Eclipse Public License version 1.0, available at
   * http://www.eclipse.org/legal/epl-v10.html
   */
  
  package org.jboss.forge.arquillian.util;
  
 import java.io.File;
Executes native system commands.

Author(s):
Lincoln Baxter, III
 
 public class NativeSystemCall
 {
   
Execute a native system command as if it were run from the given path.

Parameters:
command the system command to execute
parms the command parameters
out a print writer to which command output will be streamed
path the path from which to execute the command
Returns:
0 on successful completion, any other return code denotes failure
 
    public static int execFromPath(final File pathfinal String commandfinal String... parmsthrows IOException
    {
       try
       {
          String[] commandTokens = parms == null ? new String[1] : new String[parms.length + 1];
          commandTokens[0] = command;
 
          if (commandTokens.length > 1)
          {
             System.arraycopy(parms, 0, commandTokens, 1, parms.length);
          }
 
          ProcessBuilder builder = new ProcessBuilder(commandTokens);
          builder.directory(path);
          builder.redirectErrorStream(true);
          Process p = builder.start();
 
          InputStream stdout = p.getInputStream();
 
          Thread outThread = new Thread(new Receiver(stdout.));
          outThread.start();
          outThread.join();
 
          return p.waitFor();
 
       }
       catch (InterruptedException e)
       {
          e.printStackTrace();
          return -1;
       }
    }

   
Execute a native system command as if it were run from the given path.

Parameters:
command the system command to execute
parms the command parameters
Returns:
The started java.lang.Process
 
    public static Process exec(final String commandfinal String... parmsthrows IOException
    {
       String[] commandTokens = parms == null ? new String[1] : new String[parms.length + 1];
       commandTokens[0] = command;
 
       if (commandTokens.length > 1)
       {
          System.arraycopy(parms, 0, commandTokens, 1, parms.length);
       }
 
       ProcessBuilder builder = new ProcessBuilder(commandTokens);
       builder.redirectErrorStream(true);
       Process p = builder.start();
 
       InputStream stdout = p.getInputStream();
 
       Thread outThread = new Thread(new Receiver(stdout.));
       outThread.start();
 
       return p;
    }

   
Execute the given system command

Returns:
0 on successful completion, any other return code denotes failure
Throws:
java.lang.InterruptedException
   public static Process exec(final boolean waitfinal String commandfinal String... parms)
            throws IOExceptionInterruptedException
   {
      String[] commandTokens = parms == null ? new String[1] : new String[parms.length + 1];
      commandTokens[0] = command;
      if (commandTokens.length > 1)
      {
         System.arraycopy(parms, 0, commandTokens, 1, parms.length);
      }
      Process process = Runtime.getRuntime().exec(commandTokensnull);
      if (wait)
         process.waitFor();
      return process;
   }

   
Handles streaming output from executed Processes
   private static class Receiver implements Runnable
   {
      private final InputStream in;
      private final PrintStream out;
      public Receiver(InputStream inPrintStream out)
      {
         this. = in;
         this. = out;
      }
      @Override
      public void run()
      {
         try
         {
            byte[] buf = new byte[1024];
            int read;
            while ((read = .read(buf)) != -1)
            {
               .write(buf, 0, read);
            }
         }
         catch (IOException e)
         {
            // Don't care right now.
         }
      }
   }
New to GrepCode? Check out our FAQ X