简体   繁体   English

解压jar到指定目录

[英]Extracting jar to specified directory

I wanted to extract one of my jars to specified directory using jar command line utility.我想使用jar命令行实用程序将我的 jar 之一解压缩到指定目录。

If I understand this right -C option should to the trick but when I try如果我理解这个正确的-C选项应该可以解决问题,但是当我尝试时

jar xvf myJar.jar -C ./directoryToExtractTo

I am getting usage information from my jar utility, so I am doing something wrong.我从我的 jar 实用程序获取使用信息,所以我做错了什么。

Is the thing I want achievable with jar or do I need to manually move my jar and there invoke我想要用jar实现的事情还是我需要手动移动我的 jar 并在那里调用

jar xvf myJar.jar

jars use zip compression so you can use any unzip utility. jars 使用 zip 压缩,因此您可以使用任何解压缩实用程序。

Example:例子:

$ unzip myJar.jar -d ./directoryToExtractTo

It's better to do this.最好这样做。

Navigate to the folder structure you require导航到您需要的文件夹结构

Use the command使用命令

jar -xvf  'Path_to_ur_Jar_file'

There is no such option available in jar command itself. jar 命令本身没有这样的选项。 Look into the documentation :查看文档

-C dir Temporarily changes directories (cd dir) during execution of the jar command while processing the following inputfiles argument. -C dir 处理以下输入文件参数时,在执行 jar 命令期间临时更改目录 (cd dir)。 Its operation is intended to be similar to the -C option of the UNIX tar utility.它的操作旨在类似于 UNIX tar 实用程序的 -C 选项。 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.例如: jar uf foo.jar -C classes bar.class 更改到 classes 目录并将 bar.class 从该目录添加到 foo.jar。 The following command, jar uf foo.jar -C classes .以下命令 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. -C bin xyz.class 切换到classes目录,将classes目录下的所有文件都添加到foo.jar中(在jar文件中不创建classes目录),然后改回原目录,再切换到bin目录添加xyz.class 到 foo.jar。 If classes holds files bar1 and bar2, then here's what the jar file contains using jar tf foo.jar: META-INF/如果类包含文件 bar1 和 bar2,那么这里是使用 jar tf foo.jar 的 jar 文件包含的内容: META-INF/

META-INF/MANIFEST.MF元信息/清单.MF

bar1酒吧1

bar2酒吧2

xyz.class xyz.class

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.如果您不想更改当前的工作目录,在这样的子 shell 中运行提取命令可能会更容易。

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 ] [感谢David Schmitt的 subshel​​l 技巧]

This worked for me.这对我有用。

I created a folder then changed into the folder using CD option from command prompt.我创建了一个文件夹,然后从命令提示符使用 CD 选项更改为该文件夹。

Then executed the jar from there.然后从那里执行 jar。

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

This is what I ended up using inside my .bat file.这是我最终在 .bat 文件中使用的内容。 Windows only of course.当然只有 Windows。

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

Current working version as of Oct 2020, updated to use maven-antrun-plugin 3.0.0.截至 2020 年 10 月的当前工作版本,已更新为使用 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 ):可能有点矫枉过正,但这是我想要的,所以我为它编写了一个 Bourne Shell 脚本(依赖于sedgrep ):

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完整的脚本在这里是这个要点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM