Extracting jar to specified directory
JavaJarCommandJava Problem Overview
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
Java Solutions
Solution 1 - Java
jars use zip compression so you can use any unzip utility.
Example:
$ unzip myJar.jar -d ./directoryToExtractTo
Solution 2 - Java
It's better to do this.
Navigate to the folder structure you require
Use the command
jar -xvf 'Path_to_ur_Jar_file'
Solution 3 - Java
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
Solution 4 - Java
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 ]
Solution 5 - Java
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
Solution 6 - Java
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%
Solution 7 - Java
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>
Solution 8 - Java
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