I have created a JAR file. Now, I created another Java program. I want to unpack that JAR file in some other directory, meaning I want to do something like unzip.
If I run jar -xf filename.jar
this causes some error:
Exception in thread "main" java.io.IOException: Cannot run program "jar": java.io.IOException: error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:459) at java.lang.Runtime.exec(Runtime.java:593)`
You can always extract the source files (Java files) of a jar file into a zip. location on your system. Drag and drop the jar for which you want the sources on the JAD. 3 JAD UI will open with all the package structure in a tree format.
Adapt this example: How to extract Java resources from JAR and zip archive
Or try this code:
Extract the Contents of ZIP/JAR Files Programmatically
Suppose
jarFile
is the jar/zip file to be extracted.destDir
is the path where it will be extracted:java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile); java.util.Enumeration enumEntries = jar.entries(); while (enumEntries.hasMoreElements()) { java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement(); java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName()); if (file.isDirectory()) { // if its a directory, create it f.mkdir(); continue; } java.io.InputStream is = jar.getInputStream(file); // get the input stream java.io.FileOutputStream fos = new java.io.FileOutputStream(f); while (is.available() > 0) { // write contents of 'is' to 'fos' fos.write(is.read()); } fos.close(); is.close(); } jar.close();
Source: http://www.devx.com/tips/Tip/22124
You can use this code snippet as a reference to get your task done.Its almost the same as the code snippet shown above by @JuanZe except that for those who were getting the FileNotFoundException, i have added a small code snippet that will check if the file does exist and if it doesn't then it will create the parent folder along with the files and will extract the contents of jar file inside the specified destination folder.
Code snippet:
public class JarDemo { public static void main(String[] args) throws java.io.IOException { java.util.jar.JarFile jarfile = new java.util.jar.JarFile(new java.io.File("E:/sqljdbc4.jar")); //jar file path(here sqljdbc4.jar) java.util.Enumeration<java.util.jar.JarEntry> enu= jarfile.entries(); while(enu.hasMoreElements()) { String destdir = "E:/abc/"; //abc is my destination directory java.util.jar.JarEntry je = enu.nextElement(); System.out.println(je.getName()); java.io.File fl = new java.io.File(destdir, je.getName()); if(!fl.exists()) { fl.getParentFile().mkdirs(); fl = new java.io.File(destdir, je.getName()); } if(je.isDirectory()) { continue; } java.io.InputStream is = jarfile.getInputStream(je); java.io.FileOutputStream fo = new java.io.FileOutputStream(fl); while(is.available()>0) { fo.write(is.read()); } fo.close(); is.close(); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With