Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4794652

(process) Arg with space and trailing backslash mangled (windows)

    Details

    • Subcomponent:
    • Resolved In Build:
      04
    • CPU:
      x86
    • OS:
      windows_2000, windows_xp

      Backports

        Description

        Runtime.exec(String[]) does not correctly handle a command argument that
        contains a space and ends with a backslash.

        -- The following program demonstrates the problem

        import java.io.*;
        import java.util.*;

        public class Cmd {

            private static String getJavaCommand() {
                String javaHome = System.getProperty("java.home");
                if (javaHome != null && javaHome.length() > 0)
                    return (javaHome
                            + File.separatorChar + "bin"
                            + File.separatorChar + "java");
                else
                    return "java";
            }

            public static void main(String[] args) throws Exception {

                if (args.length > 0) {
                    System.err.println("child: " + args[0]);
                    return;
                }

                String[] cmd = new String[3];
                cmd[0] = getJavaCommand();
                cmd[1] = "Cmd";
                cmd[2] = "foo bar\\baz\\";
                System.err.println("parent: " + cmd[2]);

                Process process = Runtime.getRuntime().exec(cmd);
                InputStream in = process.getErrorStream();
                byte[] buf = new byte[1024];
                int n;
                while ((n = in.read(buf)) >= 0)
                    System.out.write(buf, 0, n);
         
            }

        }

        -- Expected output

        parent: foo bar\bazchild: foo bar\baz
        -- Actual output

        parent: foo bar\bazchild: foo bar\baz"

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  mr Mark Reinhold
                  Reporter:
                  duke J. Duke (Inactive)
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  0 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: