140

I am trying to extract the files from a .jar file. How do I do that using command line?

I am running Windows 7

Bobby C
  • 1,715
  • 3
  • 13
  • 10

8 Answers8

187

From the docs:

To extract the files from a jar file, use x, as in:

C:\Java> jar xf myFile.jar

To extract only certain files from a jar file, supply their filenames:

C:\Java> jar xf myFile.jar foo bar

The folder where jar is probably isn't C:\Java for you, on my Windows partition it's:

C:\Program Files (x86)\Java\jdk[some_version_here]\bin

Unless the location of jar is in your path environment variable, you'll have to specify the full path/run the program from inside the folder.

EDIT: Here's another article, specifically focussed on extracting JARs: http://docs.oracle.com/javase/tutorial/deployment/jar/unpack.html

acdcjunior
  • 124,334
  • 35
  • 321
  • 293
AusCBloke
  • 17,344
  • 6
  • 39
  • 44
  • Ok, When I do that jar xf... command, I get the error: 'jar' is not recognized as an internal or external command,operable program or batch file. How do I fix this? – Bobby C Dec 10 '11 at 05:09
  • 1
    @BobbyC: Specify the full path of `jar.exe` or run it from inside the folder. – AusCBloke Dec 10 '11 at 07:39
  • 3
    java.io.IOException: META-INF : could not create directory at sun.tools.jar.Main.extractFile(Main.java:928) at sun.tools.jar.Main.extract(Main.java:852) at sun.tools.jar.Main.run(Main.java:242) at sun.tools.jar.Main.main(Main.java:1149) how to remove this error? – AnilPatel Apr 24 '13 at 09:49
  • 1
    @AnilPatel I fixed this by running the command prompt as administrator (right click "run as administrator") – jessieloo Sep 10 '15 at 03:23
  • here's a stupid question - if I do this right (fixed the error messages I received first time), where are these files going to be extracted to? I've checked the jdk folder and the folder of the jar file to extract but I don't see any new files – jessieloo Sep 10 '15 at 03:33
  • @jessieloo in my case they were in the folder from which I run the command – Line Jan 03 '18 at 12:06
43

Note that a jar file is a Zip file, and any Zip tool (such as 7-Zip) can look inside the jar.

Hot Licks
  • 46,205
  • 16
  • 90
  • 149
40

In Ubuntu:

unzip file.jar -d dir_name_where_extracting

burtsevyg
  • 3,294
  • 1
  • 27
  • 38
10

You can use the following command: jar xf rt.jar

Where X stands for extraction and the f would be any options that indicate that the JAR file from which files are to be extracted is specified on the command line, rather than through stdin.

bland
  • 1,948
  • 1
  • 15
  • 22
Nikhil Arora
  • 309
  • 3
  • 9
3

jar xf myFile.jar
change myFile to name of your file
this will save the contents in the current folder of .jar file
that should do :)

abe312
  • 4,485
  • 32
  • 21
3

Java has a class specifically for zip files and one even more specifically for Jar Files.

java.util.jar.JarOutputStream
java.util.jar.JarInputStream

using those you could, on a command from the console, using a scanner set to system.in

Scanner console = new Scanner(System.in);
String input = console.nextLine();

then get all the components and write them as a file.

JarEntry JE = null;
while((JE = getNextJarEntry()) != null)
{
    //do stuff with JE
}

You can also use java.util.zip.ZipInputStream instead, as seeing a JAR file is in the same format as a ZIP file, ZipInputStream will be able to handle the Jar file, in fact JarInputStream actually extends ZipInputStream.

an alternative is also instead of getNextJarEntry, to use getNextEntry

D3_JMultiply
  • 972
  • 1
  • 10
  • 21
1

Given a file named Me.Jar:

  1. Go to cmd
  2. Hit Enter
  3. Use the Java jar command -- I am using jdk1.8.0_31 so I would type

    C:\Program Files (x86)\Java\jdk1.8.0_31\bin\jar xf me.jar

That should extract the file to the folder bin. Look for the file .class in my case my Me.jar contains a Valentine.class

Type java Valentine and press Enter and your message file will be opened.

eebbesen
  • 4,946
  • 8
  • 48
  • 68
-3

To extract the jar into specified folder use this command via command prompt

C:\Java> jar xf myFile.jar -C "C:\tempfolder"
Sergey Glotov
  • 19,939
  • 11
  • 82
  • 96
  • 7
    This doesn't work. `-C` can only be used when creating jar files. – unwichtich Jan 03 '15 at 18:06
  • @unwichtich In that case, is there a way to specify destination folder? – Line Jan 03 '18 at 12:10
  • 2
    On Linux systems you could use `unzip file.jar -d dir_name_where_extracting`, on Windows you have to copy/move your JAR to the destination folder and then extract it there... – unwichtich Jan 03 '18 at 14:09