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

Spaces in command-line parameters removed by Runtime.exec

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.2.2
    • hotspot
    • generic
    • generic



      Name: krT82822 Date: 08/13/99


      When using Runtime.exec to execute a windows console program
      (findstr.exe), passed a search parameter containing two adjacent
      spaces. Neither version of the exec method works to produce the
      same output as you get by executing the command from the
      command line, but both do generate different results, given
      the identical command. The program worked fine when using
      the string[] exec method in jdk 1.1.8 but not with the 1.2 versions

      Output of java -version:
      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      The command-line I'm trying to emit is:
         findstr /B /C:"AA. " acro.txt

      where findstr.exe is a standard windows NT console program located in the
      windows directory (execute findstr /? for parameter explanations), and
      'acro.txt' is the following 46-line text data file:

      AA Avenue of Approach
      AA Alcoholics Anonymous
      AA Automobile Association (in England)
      AAA American Automobile Association
      AAAL American Academy of Arts and Letters
      AAAS American Association for the Advancement of Science
      AAII American Association of Individual Investors
      AAMSI American Association for Medical Systems Informatics
      AAP Affirmative Action Program
      AARC Anglo-American Cataloging Rules
      ACAA Agricultural Conservation and Adjustment Administration
      AIAA American Institute of Aeronautics and Astronautics
      AMRAAM Advanced Medium Range Air-To-Air Missile
      FAA Federal Aviation Agency
      GAAP Generally Accepted Accounting Principles
      HP Higher Power (sort of an AA "in" term, usually God)
      MAAP Maintenance And Administration Panel
      NAACP National Association for the Advancement of Colored People
      NAAS North American Automated Systems co.
      NOAA National Oceanic and Atmospheric Administration
      RAAF Royal Australian Air Force
      SAA South Atlantic Anomaly
      SAA Systems Application Architecture
      AA Associate in Accounting
      AA Antiaircraft Artillery
      AA Affirmative Action committee
      AA Another Acronym
      AAA Autonomous Administrative Area
      AAI Automated AUTODIN Interface
      AAI AUTODIN/AUTOVON Interface
      AAI Administration Authority Identifier
      AAL ATM Application Layer
      AARE Application Associate REsponse
      AARNet Australian Academic Research Network
      AARP AppleTalk Address Resolution Protocol
      AARQ Application Associate ReQuest
      AARTS Automatic Audio Remote Test Set
      DAAC Distributed Active Archive Center
      GAAP Generally Accepted Accounting Principles
      ISSAA Information System Selection Acquisition Agency
      MAA MAjor synchronize Acknowledge
      SAA Systems Application Architecture
      THAAD Theater High Altitude Area Defense system
      YAA Yet Another Acronym
      TANSTAAFL There Ain't No Such Thing As A Free Lunch
      THAAD Theater High Altitude Area Defense system

      The following program executes the command first with the string
      array, then with the string:

      import java.io.*;

      public class jdcbug {
        public static void main(String argv[]) {
          Runtime rt = Runtime.getRuntime();
          Process p;
          BufferedReader br;

          // Note: 'findstr.exe' is a Microsoft executable found in /windows
          // on NT4.0 systems. See findstr /? for more info on parameters.
          // The use of a '.' in the search string specifies 'match one and
          // only one character'.
          String[] command = new String[4];
          command[0] = "findstr"; // the lookup program to execute
          command[1] = "/B"; // parameter - look at the start of the line.
          command[2] = "/C:\"AA. \""; // parameter - the literal search string.
          command[3] = "acro.txt"; // parameter - which file to look in.

          // Output 1 - array version
          try {
            p = rt.exec(command);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String resultLine = "";
            while((resultLine = br.readLine()) != null) {
              System.out.println(resultLine);
            }//end while
          } catch(IOException ioe) {
          }//end try/catch

          System.out.println("\n\n"); // output separator

          // Output 2 - string version
          String s = "findstr /B /C:\"AA. \" acro.txt";
          try {
            p = rt.exec(s);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String resultLine = "";
            while((resultLine = br.readLine()) != null) {
              System.out.println(resultLine);
            }//end while
          } catch(IOException ioe) {
          }//end try/catch
        }//end main

      }//end jdcbug

      Here is the output:

      acro.txt:AA Avenue of Approach
      acro.txt:AA Alcoholics Anonymous
      acro.txt:AA Automobile Association (in England)
      acro.txt:AAA American Automobile Association
      acro.txt:AAAL American Academy of Arts and Letters
      acro.txt:AAAS American Association for the Advancement of Science
      acro.txt:AAII American Association of Individual Investors
      acro.txt:AAMSI American Association for Medical Systems Informatics
      acro.txt:AAP Affirmative Action Program
      acro.txt:AARC Anglo-American Cataloging Rules
      acro.txt:AA Associate in Accounting
      acro.txt:AA Antiaircraft Artillery
      acro.txt:AA Affirmative Action committee
      acro.txt:AA Another Acronym
      acro.txt:AAA Autonomous Administrative Area
      acro.txt:AAI Automated AUTODIN Interface
      acro.txt:AAI AUTODIN/AUTOVON Interface
      acro.txt:AAI Administration Authority Identifier
      acro.txt:AAL ATM Application Layer
      acro.txt:AARE Application Associate REsponse
      acro.txt:AARNet Australian Academic Research Network
      acro.txt:AARP AppleTalk Address Resolution Protocol
      acro.txt:AARQ Application Associate ReQuest
      acro.txt:AARTS Automatic Audio Remote Test Set

      AA Avenue of Approach
      AA Alcoholics Anonymous
      AA Automobile Association (in England)
      AAA American Automobile Association
      AAP Affirmative Action Program
      AA Associate in Accounting
      AA Antiaircraft Artillery
      AA Affirmative Action committee
      AA Another Acronym
      AAA Autonomous Administrative Area
      AAI Automated AUTODIN Interface
      AAI AUTODIN/AUTOVON Interface
      AAI Administration Authority Identifier
      AAL ATM Application Layer

      Here is the (as expected) output when the command
      is issued from a command line:

      AAA American Automobile Association
      AAP Affirmative Action Program
      AAA Autonomous Administrative Area
      AAI Automated AUTODIN Interface
      AAI AUTODIN/AUTOVON Interface
      AAI Administration Authority Identifier
      AAL ATM Application Layer


      It seems as though the array method of exec is removing
      both spaces and also adding a 'blank' final parameter,
      and the string version is collapsing the two spaces down
      to one.
      (Review ID: 93898)
      ======================================================================

            hongzh Hong Zhang
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: