Commits

Michael Heemskerk committed 70c177b

CRUC-5325: ExternalProcess fails on Windows with commandline arguments with spaces.

  • Participants
  • Parent commits a150a7d

Comments (0)

Files changed (1)

processutils/src/main/java/com/atlassian/utils/process/ExternalProcess.java

     	return System.getProperty("os.name").toLowerCase().contains("windows");
     }
     
+    private String quoteString(String value) {
+        StringBuilder builder = new StringBuilder();
+        builder.append("\"");
+        builder.append(value.replace("\"", "\\\""));
+        builder.append("\"");
+        return builder.toString();
+    }
+    
     /*
       * This method provides a workaround for a JVM bug on windows (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4947220). The bug
       * is that the Sun/Oracle JVM uses the (8 bit) OEM codepage for encoding commandline arguments that are passed to an external process.  Any
                 }
             }
         }
-        int extraArgs = 2;
+        int extraArgs = 3;
         String[] i18n = new String[cmdArray.length + extraArgs];
         i18n[0] = "cmd";
-        i18n[1] = "/C";
+        i18n[1] = "/A"; // use ANSI encoding 
+        i18n[2] = "/C";
         i18n[extraArgs] = cmdArray[0];
         for (int counter = 1; counter < cmdArray.length; counter++) {
             String envName = "JENV_" + counter;
             i18n[counter + extraArgs] = "%" + envName + "%";
-            newEnv.put(envName, cmdArray[counter]);
+            newEnv.put(envName, quoteString(cmdArray[counter]));
         }
         cmdArray = i18n;