3030import java .io .InputStreamReader ;
3131import java .io .OutputStream ;
3232import java .io .Writer ;
33+ import java .util .ArrayList ;
3334import java .util .List ;
35+ import java .util .Map ;
36+ import java .util .StringTokenizer ;
3437
3538/**
3639 * This class defines new groovy methods which appear on normal JDK
@@ -538,7 +541,7 @@ public void run() {
538541 * @since 1.0
539542 */
540543 public static Process execute (final String self ) throws IOException {
541- return Runtime . getRuntime (). exec ( self );
544+ return new ProcessBuilder ( tokenize ( self )). start ( );
542545 }
543546
544547 /**
@@ -561,7 +564,17 @@ public static Process execute(final String self) throws IOException {
561564 * @since 1.0
562565 */
563566 public static Process execute (final String self , final String [] envp , final File dir ) throws IOException {
564- return Runtime .getRuntime ().exec (self , envp , dir );
567+ ProcessBuilder pb = new ProcessBuilder (tokenize (self ));
568+ if (dir != null ) pb .directory (dir );
569+ if (envp != null ) {
570+ Map <String , String > env = pb .environment ();
571+ env .clear ();
572+ for (String e : envp ) {
573+ int idx = e .indexOf ('=' );
574+ if (idx >= 0 ) env .put (e .substring (0 , idx ), e .substring (idx + 1 ));
575+ }
576+ }
577+ return pb .start ();
565578 }
566579
567580 /**
@@ -722,6 +735,16 @@ public static Process execute(final List commands, final List envp, final File d
722735 return Runtime .getRuntime ().exec (stringify (commands ), stringify (envp ), dir );
723736 }
724737
738+ // just simple parsing otherwise use ProcessBuilder directly
739+ private static List <String > tokenize (final String command ) {
740+ StringTokenizer st = new StringTokenizer (command );
741+ List <String > tokens = new ArrayList <>();
742+ while (st .hasMoreTokens ()) {
743+ tokens .add (st .nextToken ());
744+ }
745+ return tokens ;
746+ }
747+
725748 private static String [] stringify (final List orig ) {
726749 if (orig == null ) return null ;
727750 String [] result = new String [orig .size ()];
0 commit comments