This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new e85473e6da minor refactor: remove Runtime.exec deprecation warnings
e85473e6da is described below

commit e85473e6daf46a8ea38a5993f8c6b24e50fb0d75
Author: Paul King <[email protected]>
AuthorDate: Sat Apr 4 06:52:45 2026 +1000

    minor refactor: remove Runtime.exec deprecation warnings
---
 .../groovy/runtime/ProcessGroovyMethods.java       | 27 ++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/codehaus/groovy/runtime/ProcessGroovyMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/ProcessGroovyMethods.java
index 10b6319d08..1297621717 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ProcessGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ProcessGroovyMethods.java
@@ -30,7 +30,10 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.Writer;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
 
 /**
  * This class defines new groovy methods which appear on normal JDK
@@ -538,7 +541,7 @@ public class ProcessGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.0
      */
     public static Process execute(final String self) throws IOException {
-        return Runtime.getRuntime().exec(self);
+        return new ProcessBuilder(tokenize(self)).start();
     }
 
     /**
@@ -561,7 +564,17 @@ public class ProcessGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.0
      */
     public static Process execute(final String self, final String[] envp, 
final File dir) throws IOException {
-        return Runtime.getRuntime().exec(self, envp, dir);
+        ProcessBuilder pb = new ProcessBuilder(tokenize(self));
+        if (dir != null) pb.directory(dir);
+        if (envp != null) {
+            Map<String, String> env = pb.environment();
+            env.clear();
+            for (String e : envp) {
+                int idx = e.indexOf('=');
+                if (idx >= 0) env.put(e.substring(0, idx), e.substring(idx + 
1));
+            }
+        }
+        return pb.start();
     }
 
     /**
@@ -722,6 +735,16 @@ public class ProcessGroovyMethods extends 
DefaultGroovyMethodsSupport {
         return Runtime.getRuntime().exec(stringify(commands), stringify(envp), 
dir);
     }
 
+    // just simple parsing otherwise use ProcessBuilder directly
+    private static List<String> tokenize(final String command) {
+        StringTokenizer st = new StringTokenizer(command);
+        List<String> tokens = new ArrayList<>();
+        while (st.hasMoreTokens()) {
+            tokens.add(st.nextToken());
+        }
+        return tokens;
+    }
+
     private static String[] stringify(final List orig) {
         if (orig == null) return null;
         String[] result = new String[orig.size()];

Reply via email to