将 jar 解压缩到指定的目录

我想使用 jar命令行实用程序将一个 jar 解压缩到指定的目录。

如果我理解这个正确的 -C选项应该把戏,但当我尝试

jar xvf myJar.jar -C ./directoryToExtractTo

我正在从 jar 实用程序获取使用信息,所以我正在做一些错误的事情。

是我希望用 jar实现的东西,还是需要手动移动我的 jar 并在那里调用

jar xvf myJar.jar
159959 次浏览

It's better to do this.

Navigate to the folder structure you require

Use the command

jar -xvf  'Path_to_ur_Jar_file'

jars use zip compression so you can use any unzip utility.

Example:

$ unzip myJar.jar -d ./directoryToExtractTo

There is no such option available in jar command itself. Look into the documentation:

-C dir Temporarily changes directories (cd dir) during execution of the jar command while processing the following inputfiles argument. Its operation is intended to be similar to the -C option of the UNIX tar utility. For example: jar uf foo.jar -C classes bar.class changes to the classes directory and add the bar.class from that directory to foo.jar. The following command, jar uf foo.jar -C classes . -C bin xyz.class changes to the classes directory and adds to foo.jar all files within the classes directory (without creating a classes directory in the jar file), then changes back to the original directory before changing to the bin directory to add xyz.class to foo.jar. If classes holds files bar1 and bar2, then here's what the jar file contains using jar tf foo.jar: META-INF/

META-INF/MANIFEST.MF

bar1

bar2

xyz.class

This worked for me.

I created a folder then changed into the folder using CD option from command prompt.

Then executed the jar from there.

d:\LS\afterchange>jar xvf ..\mywar.war

This is what I ended up using inside my .bat file. Windows only of course.

set CURRENT_DIR=%cd%
mkdir ./directoryToExtractTo
cd ./directoryToExtractTo
jar xvf %CURRENT_DIR%\myJar.jar
cd %CURRENT_DIR%

In case you don't want to change your current working directory, it might be easier to run extract command in a subshell like this.

mkdir -p "/path/to/target-dir"
(cd "/path/to/target-dir" && exec jar -xf "/path/to/your/war-file.war")

You can then execute this script from any working directory.

[ Thanks to David Schmitt for the subshell trick ]

Current working version as of Oct 2020, updated to use maven-antrun-plugin 3.0.0.

        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>prepare</id>
<phase>package</phase>
<configuration>
<target>
<unzip src="target/shaded-jar/shade-test.jar"
dest="target/unpacked-shade/"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Probably a bit of overkill, but this is something I wanted, so I wrote a Bourne Shell script for it (dependencies on sed and grep):

Usage: unjar FILE [DEST]


- DEST must not already exist (won't overwrite existing contents);
path to DEST will be created.


- If DEST is not provided, defaults to a subdirectory in the current
working directory that will be named after FILE without the extension.
Ex: unjar foo.jar  # foo.jar extracted to ./foo


- If DEST is provided, the path will be created and the jar will
be extracted into it.
Ex: unjar foo.jar /a/b/c  # foo.jar extracted into /a/b/c

The full script is here at this gist