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

(process spec) clarify exception behavior of Runtime.exec("")

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      Name: viR10068 Date: 04/23/2004


      The spec says for the method java.lang.Runtime.exec(String):
      "...
      This is a convenience method. An invocation of the form exec(command)
      behaves in exactly the same way as the invocation exec(command, null, null).
      ...
      Throws:
      IllegalArgumentException - If command is empty"

      The spec says for the method java.lang.Runtime.exec(String, String[], File):
      "An invocation of the form exec(command, envp, dir) behaves in exactly the
      same way as the invocation exec(cmdarray, envp, dir), where cmdarray is an
      array of all the tokens in command.
       More precisely, the command string is broken into tokens using a StringTokenizer
      created by the call new StringTokenizer(command) with no further modification of
      the character categories. The tokens produced by the tokenizer are then placed in
      the new string array cmdarray, in the same order.
      ...
      Throws:
      IllegalArgumentException - If command is empty"

      The spec says for the method java.lang.Runtime.exec(String[], String[], File):
      "...
      Throws:
      IndexOutOfBoundsException - If cmdarray is an empty array (has length 0)"

      So, according to the spec the method java.lang.Runtime.exec("") can
      throw 2 different exception for 1 error:
      1) the IllegalArgumentException: as described in the Throws clause.
      2) the IndexOutOfBoundsException: according to the statement "behaves in exactly
      the same way as the invocation exec(command, null, null)".
      Note, that the (new StringTokenizer("")).countTokens() return 0 and constructed
      array of all the tokens in command is an empty array.

      The words "behaves in exactly the same way" in the exec(String, String[], File)
      method description contradicts to the Throws clause and may mislead user.
      Some clarification is needed. For example:
      "behaves in exactly the same way (except the case when command is empty)"

      Note that the Sun's RI throws the IndexOutOfBoundsException for jdk up
      to 1.4.0 and throws the IllegalArgumentException starting from the jdk1.4.1:
      % java -showversion Hello
      java version "1.4.1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
      Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

      Exception in thread "main" java.lang.IllegalArgumentException: Empty command
              at java.lang.Runtime.exec(Runtime.java:417)
              at java.lang.Runtime.exec(Runtime.java:364)
              at java.lang.Runtime.exec(Runtime.java:326)
              at Hello.main(Hello.java:7)
      % java -showversion Hello
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      Exception in thread "main" java.lang.IndexOutOfBoundsException
              at java.lang.Runtime.exec(Runtime.java:531)
              at java.lang.Runtime.exec(Runtime.java:416)
              at java.lang.Runtime.exec(Runtime.java:358)
              at java.lang.Runtime.exec(Runtime.java:322)
              at Hello.main(Hello.java:7)
      %

      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            martin Martin Buchholz
            Reporter:
            vivsunw Viv Viv (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: