src/share/classes/java/lang/ProcessBuilder.java

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;
  32 import java.io.FileOutputStream;
  33 import java.security.AccessControlException;
  34 import java.util.Arrays;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Map;
  38 
  39 /**
  40  * This class is used to create operating system processes.
  41  *
  42  * <p>Each {@code ProcessBuilder} instance manages a collection
  43  * of process attributes.  The {@link #start()} method creates a new
  44  * {@link Process} instance with those attributes.  The {@link
  45  * #start()} method can be invoked repeatedly from the same instance
  46  * to create new subprocesses with identical or related attributes.
  47  *
  48  * <p>Each process builder manages these process attributes:
  49  *
  50  * <ul>
  51  *
  52  * <li>a <i>command</i>, a list of strings which signifies the


1007         cmdarray = cmdarray.clone();
1008 
1009         for (String arg : cmdarray)
1010             if (arg == null)
1011                 throw new NullPointerException();
1012         // Throws IndexOutOfBoundsException if command is empty
1013         String prog = cmdarray[0];
1014 
1015         SecurityManager security = System.getSecurityManager();
1016         if (security != null)
1017             security.checkExec(prog);
1018 
1019         String dir = directory == null ? null : directory.toString();
1020 
1021         try {
1022             return ProcessImpl.start(cmdarray,
1023                                      environment,
1024                                      dir,
1025                                      redirects,
1026                                      redirectErrorStream);
1027         } catch (IOException e) {
1028             String exceptionInfo = ": " + e.getMessage();
1029             Throwable cause = e;
1030             if (security != null) {
1031                 // Can not disclose the fail reason for read-protected files.
1032                 try {
1033                     security.checkRead(prog);
1034                 } catch (AccessControlException ace) {
1035                     exceptionInfo = "";
1036                     cause = ace;
1037                 }
1038             }
1039             // It's much easier for us to create a high-quality error
1040             // message than the low-level C code which found the problem.
1041             throw new IOException(
1042                 "Cannot run program \"" + prog + "\""
1043                 + (dir == null ? "" : " (in directory \"" + dir + "\")")
1044                 + exceptionInfo,
1045                 cause);
1046         }
1047     }
1048 }


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;

  32 import java.security.AccessControlException;
  33 import java.util.Arrays;
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 import java.util.Map;
  37 
  38 /**
  39  * This class is used to create operating system processes.
  40  *
  41  * <p>Each {@code ProcessBuilder} instance manages a collection
  42  * of process attributes.  The {@link #start()} method creates a new
  43  * {@link Process} instance with those attributes.  The {@link
  44  * #start()} method can be invoked repeatedly from the same instance
  45  * to create new subprocesses with identical or related attributes.
  46  *
  47  * <p>Each process builder manages these process attributes:
  48  *
  49  * <ul>
  50  *
  51  * <li>a <i>command</i>, a list of strings which signifies the


1006         cmdarray = cmdarray.clone();
1007 
1008         for (String arg : cmdarray)
1009             if (arg == null)
1010                 throw new NullPointerException();
1011         // Throws IndexOutOfBoundsException if command is empty
1012         String prog = cmdarray[0];
1013 
1014         SecurityManager security = System.getSecurityManager();
1015         if (security != null)
1016             security.checkExec(prog);
1017 
1018         String dir = directory == null ? null : directory.toString();
1019 
1020         try {
1021             return ProcessImpl.start(cmdarray,
1022                                      environment,
1023                                      dir,
1024                                      redirects,
1025                                      redirectErrorStream);
1026         } catch (IOException | IllegalArgumentException e) {
1027             String exceptionInfo = ": " + e.getMessage();
1028             Throwable cause = e;
1029             if ((e instanceof IOException) && security != null) {
1030                 // Can not disclose the fail reason for read-protected files.
1031                 try {
1032                     security.checkRead(prog);
1033                 } catch (AccessControlException ace) {
1034                     exceptionInfo = "";
1035                     cause = ace;
1036                 }
1037             }
1038             // It's much easier for us to create a high-quality error
1039             // message than the low-level C code which found the problem.
1040             throw new IOException(
1041                 "Cannot run program \"" + prog + "\""
1042                 + (dir == null ? "" : " (in directory \"" + dir + "\")")
1043                 + exceptionInfo,
1044                 cause);
1045         }            
1046     }
1047 }