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

237,708

Solution 1

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.

Solution 2

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

Solution 3

Can't you just change working directory within the python script using os.chdir(target)? I agree, I can't see any way of doing it from the jar command itself.

If you don't want to permanently change directory, then store the current directory (using os.getcwd())in a variable and change back afterwards.

Solution 4

If this is a personal script, rather than one you're planning on distributing, it might be simpler to write a shell function for this:

function warextract { jar xf $1 $2 && mv $2 $3 }

which you could then call from python like so:

warextract /home/foo/bar/Portal.ear Binaries.war /home/foo/bar/baz/

If you really feel like it, you could use sed to parse out the filename from the path, so that you'd be able to call it with

warextract /home/foo/bar/Portal.ear /home/foo/bar/baz/Binaries.war

I'll leave that as an excercise to the reader, though.

Of course, since this will extract the .war out into the current directory first, and then move it, it has the possibility of overwriting something with the same name where you are.

Changing directory, extracting it, and cd-ing back is a bit cleaner, but I find myself using little one-line shell functions like this all the time when I want to reduce code clutter.

Share:
237,708
Christopher Tokar
Author by

Christopher Tokar

Front-end focused senior developer.

Updated on July 14, 2020

Comments

  • Christopher Tokar
    Christopher Tokar almost 4 years

    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.

  • zenazn
    zenazn almost 15 years
    Yeah--.jars are just .zips with a different extension. When "jar" tools are lacking, there are plenty of tools that deal with zips that can do just about anything under the sun (ha ha bad pun)
  • nos
    nos almost 15 years
    This is the way to do it it. You could also create the directory first if it doesn't exist.
  • Christopher Tokar
    Christopher Tokar almost 15 years
    This would work, but the Portal.ear file is huge and would take too long. I just want to extract the Binaries.war archive from Portal.ear. There must be a way to do that using unzip.
  • Jonathan Leffler
    Jonathan Leffler almost 15 years
    Run "mkdir -p /where/you/want/it" before the 'cd' command - yes, certainly you could do that.
  • MestreLion
    MestreLion about 11 years
    This function first extracts to current directory and then it moves to destination. So one can only use it while in a directory you have write permissions to, which is a let down. Is there a way to tell jar to extract directly to the destination?
  • Xiong Chiamiov
    Xiong Chiamiov over 10 years
    jar should support the -C (change directory) option on extraction, but it doesn't on my machine (which I find strange). Due to that, I'd probably rewrite the function today to something more like function warextract { pushd "$3" && jar xf "$1" "$2" ; popd }. Well, and add --help documentation. :)
  • Xiong Chiamiov
    Xiong Chiamiov over 10 years
    Also, basename and dirname are much better options for doing directory/file splits than sed. The things you learn in four years.
  • Carlos
    Carlos over 9 years
    In bash, I had to write this function (notice the parenthesis) like this instead: function warextract (){ jar xf $1 $2 && mv $2 $3 }
  • Xiong Chiamiov
    Xiong Chiamiov over 9 years
    You should never use both function and (); just parens is the safest choice.
  • Tung
    Tung over 8 years
    I confirm that this way is simpler than I have ever seen. Indeed, jar is a kind of achieve file.
  • Cale Sweeney
    Cale Sweeney about 7 years
    In case any of you are concerned, this jar xf method leaves the original jar intact.