Andna
Andna

Reputation: 6689

Extracting jar to specified directory

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 trick but when I try

jar xvf myJar.jar -C ./directoryToExtractTo

I am getting usage information from my jar utility, so I am doing something wrong.

Is the thing I want achievable with jar or do I need to manually move my jar and there invoke

jar xvf myJar.jar

Upvotes: 97

Views: 190808

Answers (8)

Subfuzion
Subfuzion

Reputation: 1929

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

Upvotes: 0

Will Iverson
Will Iverson

Reputation: 2079

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>

Upvotes: 0

Bienvenido David
Bienvenido David

Reputation: 4348

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%

Upvotes: 2

Montri M
Montri M

Reputation: 1766

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 ]

Upvotes: 9

Amit Maniar
Amit Maniar

Reputation: 1196

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

Example:

$ unzip myJar.jar -d ./directoryToExtractTo

Upvotes: 103

Ashok Ogirala
Ashok Ogirala

Reputation: 151

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

Upvotes: 5

vbg
vbg

Reputation: 768

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

Upvotes: 17

Harshavardhan Konakanchi
Harshavardhan Konakanchi

Reputation: 4294

It's better to do this.

Navigate to the folder structure you require

Use the command

jar -xvf  'Path_to_ur_Jar_file'

Upvotes: 78

Related Questions