Add an option to the jar tool to allow extracting the jar to a specific directory.
The jar tool currently extracts the jar contents into the current working directory. Other 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 target 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 target directory.
A recent 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 does 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. This new log message will be printed, when -v is used, even if the -C or --dir option hasn't been used and the jar is being extracted by default to the current working directory.
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 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.
jar -x -f somejar.jar -C /tmp/foo/bar/
This command extracts the somejar.jar file to the /tmp/foo/bar/ directory, creating it 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.
The current version of this implementation resides at https://github.com/openjdk/jdk/pull/2752