View RSS Feed

My Java Tips

How To Modify A JAR File

Rate this Entry
by , 11-08-2011 at 06:40 PM (1685 Views)
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.



Name of the JAR file.
The new file to be added.

Java Code:
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);

}

}

}

Submit "How To Modify A JAR File" to Facebook Submit "How To Modify A JAR File" to Digg Submit "How To Modify A JAR File" to del.icio.us Submit "How To Modify A JAR File" to StumbleUpon Submit "How To Modify A JAR File" to Google

Tags: jar file Add / Edit Tags
Categories
Java SE

Comments