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

jar tool should have a way to extract to a directory



    • CSR
    • Resolution: Unresolved
    • P3
    • 24
    • tools
    • None
    • jar
    • behavioral
    • minimal
    • add/remove/modify command line option
    • JDK



      Add an option to the jar tool to allow extracting the contents of the jar file to a specific directory.


      The JDK ships with a jar tool which can be used to extract the contents of a jar file. In its current form, the jar tool is capable of extracting the contents only into the current working directory. External tools like unzip, tar and such allow users to optionally specify a directory to which the user can extract the archive. Enhancing the jar tool to optionally allow specifying a destination directory during extraction will make the usage of the command easier and users won't have to keep switching between directories to extract the jar to the right directory.


      An optional -C option will be allowed to be used with the -x main operation. The -C option will take a path on the filesystem to which the jar will be extracted. The long form of this -C option will be --dir. Users can either use -C or --dir option when specifying the destination directory.

      A mailing list discussion going over the possibilities of this and other alternate option names can be found here https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-February/074730.html and (continued into next month) here https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-March/074850.html


      Optional -C and --dir options are added to the -x main operation of the jar tool. The value for these options is expected to be a file path which is expected to be a directory to which the jar will be extracted. When neither -C nor --dir are used, the extract operation of the jar tool will continue to behave like it did before this enhancement and will extract the jar into the current working directory.

      If the directory (hierarchy) passed as a value to the -C or --dir option isn't present on the filesystem, then the jar tool, during the extraction of the jar, will create those directories before extracting the jar into it.

      Users are allowed to specify either absolute or relative paths as a value to these options.

      These new options will be allowed to be used in the legacy compatibility mode where multiple options can be clubbed together using a single hyphen character. Specifically, a command like:

      jar -xvf somejar.jar -C /tmp/foo/

      will work fine and the jar will be extracted into /tmp/foo directory.

      This enhancement will also introduce a new log message which prints the directory into which the jar will be extracted. This log message will only be printed when verbose option (-v) is used during the jar extraction.

      It's an error to use -C or --dir more than once with the -x main operation of the jar tool. It's also an error to use both -C and --dir in the same command with the -x main operation. The tool will throw an error when it encounters such a situation.

      The -P option, which was introduced in the jar tool several releases back for backward compatibility, and which preserves leading slashes and any dot-dot ".." characters in the jar entry names will not be allowed to be used with the -C or --dir option. An error will be thrown by the jar tool if -P is used together with either -C or --dir option.

      Some usage examples of this new -C/--dir option follow:

      jar -x -f somejar.jar -C /tmp/foo/bar/

      This command extracts the somejar.jar file to the /tmp/foo/bar/ directory, creating the directory if necessary.

      jar -x -f somejar.jar --dir /tmp/foo/bar/

      Same as above, except uses the long form --dir option.

      jar -x -f somejar.jar -C /tmp/foo/bar/ f1.txt d1/f2.txt

      Assuming somejar.jar contains f1.txt (at root of the jar), d1/f2.txt and other files, then the above command extracts only f1.txt and d1/f2.txt into the /tmp/foo/bar/ directory.

      jar --help has been updated to update the -C option description and introduce --dir option in the help text:

      Operation modifiers valid in any mode:
        -C DIR                     Change to the specified directory and include the
                                   following file. When used in extract mode, extracts
                                   the jar to the specified directory
      Operation modifiers valid only in extract mode:
        --dir                    Directory into which the jar will be extracted


        Issue Links



              jpai Jaikiran Pai
              alanb Alan Bateman
              0 Vote for this issue
              0 Start watching this issue