简体   繁体   中英

Extracting .jar file with command line

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

I am running Windows 7

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

请注意,jar 文件是一个 Zip 文件,任何 Zip 工具(例如 7-Zip)都可以查看 jar 的内部。

在 Ubuntu 中:

unzip file.jar -d dir_name_where_extracting

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.

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 :)

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

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.

I had the same issue and the jar command did not work. I made a copy of the jar file and changed the extension to.zip. Then right click on the.zip file and select 'Extract All'. This extracted all the files.

要将 jar 解压缩到指定文件夹中,请通过命令提示符使用此命令

C:\Java> jar xf myFile.jar -C "C:\tempfolder"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM