How To Modify A JAR File

12 April 2007

This assumes a basic understanding about the JAR API’s provided by Sun, To know more click here. Now this is a step ahead of that, by which you will be able to add or delete a particular file to the JAR archive.

Listed below is an example that adds a file to an existing JAR or ZIP file. In the program an intermediate file is created first that will be later replaced with an existing one. To be more precise the intermediate file will be renamed to original one after deleting the old original one.

Listed below is the program. Program takes two command line arguments.

  1. Name of the JAR file.
  2. The new file to be added.
import java.io.BufferedReader;
 
import java.io.File;
 
import java.io.FileInputStream;
 
import java.io.FileOutputStream;
 
import java.io.IOException;
 
import java.io.InputStream;
 
import java.io.InputStreamReader;
 
import java.util.Enumeration;
 
import java.util.jar.JarEntry;
 
import java.util.jar.JarFile;
 
import java.util.jar.JarOutputStream;   public class EditJarFile {
 
public static void main(String args[]) {
 
 if (args.length != 2) {
 
    System.err.println("command: java EditJarFile sample.jar addsample.txt");
 
    System.exit(-1);
 
}
 
String jartoEdit = args[0];
 
String newFile = args[1];
 
File tempJar = null;
 
try {
 
  tempJar = File.createTempFile(jartoEdit, null);
 
} catch (IOException e) {
 
  System.err.println("Unable to create intermediate file.");
 
  System.exit(-2);
 
}
 
  JarFile jar = null;
 
try {
 
   jar = new JarFile(jartoEdit);
 
} catch (IOException e) {
 
   System.err.println("Unable to access original file.");
 
   System.exit(-3);
 
}
 
boolean delFlag = false;
 
try {
 
JarOutputStream newJar =
 
new JarOutputStream(
 
new FileOutputStream(tempJar));
 
byte buffer[] = new byte[1024];
 
int bytesRead;
 
try {
 
FileInputStream fis = new FileInputStream(newFile);
 
Enumeration entries = jar.entries();
 
while (entries.hasMoreElements()) {
 
JarEntry entry = (JarEntry) entries.nextElement();
 
String name = entry.getName();
 
System.out.println
 
("Copy " + name + " into new jar? (y or n)");
 
BufferedReader reader =
 
new BufferedReader
 
(new InputStreamReader(System.in));
 
String line = reader.readLine();
 
if ("n".equals(line)) {
 
System.out.println("Skipping: " + name);
 
continue;
 
}
 
InputStream is = jar.getInputStream(entry);
 
newJar.putNextEntry(entry);
 
while ((bytesRead = is.read(buffer)) != -1) {
 
newJar.write(buffer, 0, bytesRead);
 
}
 
}
 
try {
 
JarEntry entry = new JarEntry(newFile);
 
newJar.putNextEntry(entry);
 
while ((bytesRead = fis.read(buffer)) != -1) {
 
newJar.write(buffer, 0, bytesRead);
 
}
 
} finally {
 
fis.close();
 
}
 
delFlag = true;
 
} catch (IOException ex) {
 
System.err.println
 
("Operation aborted due to : " + ex);
 
} finally {
 
try {
 
newJar.close();
 
} catch (IOException ignored) {
 
}
 
}
 
} catch (IOException ex) {
 
System.err.println(
 
"Can't access new file");
 
} finally {
 
try {
 
jar.close();
 
} catch (IOException ignored) {
 
}
 
if (!delFlag) {
 
tempJar.delete();
 
}
 
}
 
if (delFlag) {
 
File origFile = new File(jartoEdit);
 
origFile.delete();
 
tempJar.renameTo(origFile);
 
}
 
}
 
}

del.icio.us:How To Modify A JAR File  digg:How To Modify A JAR File  spurl:How To Modify A JAR File  wists:How To Modify A JAR File  simpy:How To Modify A JAR File  newsvine:How To Modify A JAR File  blinklist:How To Modify A JAR File  furl:How To Modify A JAR File  reddit:How To Modify A JAR File  fark:How To Modify A JAR File  blogmarks:How To Modify A JAR File  Y!:How To Modify A JAR File  smarking:How To Modify A JAR File  magnolia:How To Modify A JAR File  segnalo:How To Modify A JAR File  gifttagging:How To Modify A JAR File

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: How To Modify A JAR File

One Response to “How To Modify A JAR File”

  1. RGV Says:

    Hi!

    Works good, but if the JAR belongs to the CLASSPATH env (or via de -cp xyz.jar) it’s impossible to deal with it, correct?

    Once I had this problem …

    RGV

Leave a Reply