Uploaded image for project: 'Commons Exec'
  1. Commons Exec
  2. EXEC-30

Make environment variables respect casing rules of platforms

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 1.0
    • 1.0
    • None
    • Windows

    Description

      Intro: Environment variables on Windows are case-insensitive (see ExpandEnvironmentStrings). As a little experiment, run the following code snippet on Windows with Java 1.5

      System.out.println(System.getenv("PATH"));
      System.out.println(System.getenv("path"));
      System.out.println(System.getenv("Path"));
      System.out.println(System.getenv().get("PATH"));
      System.out.println(System.getenv().get("path"));
      System.out.println(System.getenv().get("Path"));
      

      which can for example output

      <successfully resolved variable>
      <successfully resolved variable>
      <successfully resolved variable>
      null
      null
      <successfully resolved variable>

      IMHO, this is an example of how an API should not be designed. The two overloads of System.getenv() look very similar but behave completely different. Even having this documented, this subtle peculiarity makes Java programming prone to errors that prevent platform-independence.

      Commons Exec is currently providing a case-sensitive view of the Windows shell environment. This makes it effectively impossible for client to code to reliably lookup an environment variable without performing further tweaks to the map returned from EnvironmentUtil. Furthermore, these tweaks would have to be performed only depending on the current OS, so the client code would need to consider platform details. I believe this violates the basic principle of platform-independence intended by Java.

      So here my suggestion: Have Commons Exec provide a case-preserving map that obeys the casing rules of the platform for key lookup. This way, the simple code line

      EnvironmentUtils.getProcEnvironment().get("PATH")
      

      will consistently return the specified variable on Windows and Unix.

      Just to mention it: An alternative approach might be to have the map always be case-sensitive but normalize variable names to upper-case on case-insensitive platforms.

      Regardless how this issue ends, the documentation for getProcEnvironment() should clearly state how it deals with casing.

      Attachments

        1. EXEC-30.patch
          5 kB
          Benjamin Bentmann

        Activity

          People

            sgoeschl Siegfried Goeschl
            bentmann Benjamin Bentmann
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: