tags:

views:

911

answers:

4

I have created a jar file now i created another java program. I want to unpack that jar file in some other directory means 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)

+1  A: 

Your title doesn't seem to match the question very well but if you really do want to "write [a] java program extracting a jar file" you just need Class JarFile.

DigitalRoss
yes i want to write a java program which will extract a jar file
Deepak
A: 

JarFile class.

JarFile file = new JarFile("file.jar");   
for (Enumeration<JarEntry> enum = file.entries(); enum.hasMoreElements();) {   
    JarEntry entry = enum.next();   
    System.out.println(entry.getName());   
}
adatapost
Yes I want something like this but how can i store the extracted file in some other directory. Can you please guide me.
Deepak
+2  A: 

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 enum = jar.entries();
while (enum.hasMoreElements()) {
    java.util.jar.JarEntry file = (java.util.jar.JarEntry) enum.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();
}
 source: [http://www.devx.com/tips/Tip/22124][2]
JuanZe
A: 

Provide the full path to "jar" in your command line.

Thorbjørn Ravn Andersen