Extracting jar to specified directory

前端 未结 7 473
庸人自扰
庸人自扰 2021-01-31 06:58

I wanted to extract one of my jars to specified directory using jar command line utility.

If I understand this right -C option should to the tr

相关标签:
7条回答
  • 2021-01-31 07:22

    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

    0 讨论(0)
  • 2021-01-31 07:22

    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 ]

    0 讨论(0)
  • 2021-01-31 07:23

    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%
    
    0 讨论(0)
  • 2021-01-31 07:26

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

    Example:

    $ unzip myJar.jar -d ./directoryToExtractTo

    0 讨论(0)
  • 2021-01-31 07:26

    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>
    
    0 讨论(0)
  • 2021-01-31 07:44

    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
    
    0 讨论(0)
提交回复
热议问题