1 10 11 package java.lang; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 105 106 public final class ProcessBuilder 107 { 108 private List <String > command; 109 private File directory; 110 private Map <String ,String > environment; 111 private boolean redirectErrorStream; 112 113 127 public ProcessBuilder(List <String > command) { 128 if (command == null) 129 throw new NullPointerException (); 130 this.command = command; 131 } 132 133 144 public ProcessBuilder(String ... command) { 145 this.command = new ArrayList <String >(command.length); 146 for (String arg : command) 147 this.command.add(arg); 148 } 149 150 164 public ProcessBuilder command(List <String > command) { 165 if (command == null) 166 throw new NullPointerException (); 167 this.command = command; 168 return this; 169 } 170 171 182 public ProcessBuilder command(String ... command) { 183 this.command = new ArrayList <String >(command.length); 184 for (String arg : command) 185 this.command.add(arg); 186 return this; 187 } 188 189 197 public List <String > command() { 198 return command; 199 } 200 201 270 public Map <String ,String > environment() { 271 SecurityManager security = System.getSecurityManager(); 272 if (security != null) 273 security.checkPermission(new RuntimePermission ("getenv.*")); 274 275 if (environment == null) 276 environment = ProcessEnvironment.environment(); 277 278 assert environment != null; 279 280 return environment; 281 } 282 283 ProcessBuilder environment(String [] envp) { 285 assert environment == null; 286 if (envp != null) { 287 environment = ProcessEnvironment.emptyEnvironment(envp.length); 288 assert environment != null; 289 290 for (String envstring : envp) { 291 296 if (envstring.indexOf((int) '\u0000') != -1) 298 envstring = envstring.replaceFirst("\u0000.*", ""); 299 300 int eqlsign = 301 envstring.indexOf('=', ProcessEnvironment.MIN_NAME_LENGTH); 302 if (eqlsign != -1) 304 environment.put(envstring.substring(0,eqlsign), 305 envstring.substring(eqlsign+1)); 306 } 307 } 308 return this; 309 } 310 311 323 public File directory() { 324 return directory; 325 } 326 327 340 public ProcessBuilder directory(File directory) { 341 this.directory = directory; 342 return this; 343 } 344 345 359 public boolean redirectErrorStream() { 360 return redirectErrorStream; 361 } 362 363 377 public ProcessBuilder redirectErrorStream(boolean redirectErrorStream) { 378 this.redirectErrorStream = redirectErrorStream; 379 return this; 380 } 381 382 435 public Process start() throws IOException { 436 String [] cmdarray = command.toArray(new String [command.size()]); 439 for (String arg : cmdarray) 440 if (arg == null) 441 throw new NullPointerException (); 442 String prog = cmdarray[0]; 444 445 SecurityManager security = System.getSecurityManager(); 446 if (security != null) 447 security.checkExec(prog); 448 449 String dir = directory == null ? null : directory.toString(); 450 451 return ProcessImpl.start(cmdarray, 452 environment, 453 dir, 454 redirectErrorStream); 455 } 456 } 457 | Popular Tags |