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

Runtime.exec(String[]) collapses white spaces on Win32

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 1.2.0
    • 1.1, 1.1.2, 1.1.3, 1.1.5
    • core-libs
    • 1.2beta3
    • x86, sparc
    • solaris_2.4, solaris_2.6, windows_95, windows_nt
    • Not verified

    Description

      Problem:
      I have a java program that needs to exec another java program, and some of the args in the args[] must contain spaces (and other special chars).

      Solution:
      Use Runtime.exec(String[]).

      This works correctly on Solaris, but not on Win32.
      Apparently on Windows, it concatenates my String array and reparses, which is extremely problematic when the strings contain spaces.


      Here is the test code:

      import java.io.*;

      public class EA1 {

        public static void main(String[] args) throws Exception {
          String jav = System.getProperty("java.home")+File.separator+"bin"+File.separator+"java";
          String cl = "EA2";
          String arg = "X Y";
          String[] argsx = {jav,cl,arg};
          Process p = Runtime.getRuntime().exec(argsx);
          InputStream is = p.getErrorStream();
          int x = is.read();
          while(x!=-1){
            System.err.write(x);
            x = is.read();
          }
          p.waitFor();
        }

      }


      public class EA2 {

        public static void main(String[] args) throws Exception {
          System.err.println("EA2:");
          for(int i=0;i<args.length;i++){
            System.err.println(i+": "+args[i]);
          }
          System.exit(0);
        }

      }


      On Solaris:
      % java EA1
      EA2:
      0: X Y


      On Windows:
      > java EA1
      EA2:
      0: X
      1: Y


      This is not pretty.

      [fred.oliver@east]:
      This is a hot issue for the JMAPI group right now. We need to invoke one java
      program from another on Win32, where the default classpath contains spaces, e.g.
      "c:\Program Files\...".

      Since the jre command on Win32 ignores the classpath variable (I would really
      like to see the justification for that), we are required to supply the
      classpath as an argument on the command line.


      Attachments

        Issue Links

          Activity

            People

              hongzh Hong Zhang
              duke J. Duke
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: