To extract the jar into specified folder use this command via command prompt
C:\Java> jar xf myFile.jar -C "C:\tempfolder"
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 :)
Note that a jar file is a Zip file, and any Zip tool (such as 7-Zip) can look inside the jar.
Given a file named Me.Jar:
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.
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.
In Ubuntu:
unzip file.jar -d dir_name_where_extracting
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
Source: Stackoverflow.com