Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you extract a JAR in a UNIX filesystem with a single command and specify its target directory using the JAR command?

I am creating a Python script within which I am executing UNIX system commands. I have a war archive named Binaries.war which is within an ear archive named Portal.ear

The Portal ear file resides in, say /home/foo/bar/

jar xf /home/foo/bar/Portal.ear Binaries.war 

Will extract the Binaries.war file out of the /home/foo/bar/Portal.ear archive into the current directory I am running the script from.

How do you specify a target directory to be extracted to using just one command? I would like to do something like this to extract Binaries.war into the directory /home/foo/bar/baz

jar xf /home/foo/bar/Portal.ear Binaries.war [into target directory /home/foo/bar/baz] 

I searched the the JAR man page for options and can't seem to find a simple way to do this. Of course I can extract the archive into my current directory and then move it using mv but I'd like to do this in one shot and not shuffle directories and files around.

like image 233
Christopher Tokar Avatar asked Jul 03 '09 14:07

Christopher Tokar


People also ask

How do I extract a JAR file?

JAR files work just like ZIP files. You can use any archive program to extract them. On Windows, you can Install WinRAR 7-Zip, or WinZIP. Macs have their own built-in archive program called Archive Utility.

How do I search for a specific JAR file in Linux?

find ./ -name "filename" or you can do something like find ./ -name "*. jar" to find all the files with the . jar extension. You can also do find ./ -name "*.


2 Answers

If your jar file already has an absolute pathname as shown, it is particularly easy:

cd /where/you/want/it; jar xf /path/to/jarfile.jar 

That is, you have the shell executed by Python change directory for you and then run the extraction.

If your jar file does not already have an absolute pathname, then you have to convert the relative name to absolute (by prefixing it with the path of the current directory) so that jar can find it after the change of directory.

The only issues left to worry about are things like blanks in the path names.

like image 111
Jonathan Leffler Avatar answered Oct 13 '22 01:10

Jonathan Leffler


I don't think the jar tool supports this natively, but you can just unzip a JAR file with "unzip" and specify the output directory with that with the "-d" option, so something like:

$ unzip -d /home/foo/bar/baz /home/foo/bar/Portal.ear Binaries.war 
like image 21
tddmonkey Avatar answered Oct 13 '22 02:10

tddmonkey