Updating .class file in jar
This tutorial details how to update a jar file
jar -uf jar-file <optional_folder_structure>/input-file(s)
where 'u' means update.
How can I replace a class file in jar files?
In Eclipse i think it should be enough to export your code as jar file.
http://viralpatel.net/blogs/create-jar-file-in-java-eclipse/
So basically you get the old jar, extract, load as project in eclipse and export as jar file.
And you're done.
Replace a single file from jar file
Maybe the jar -uf
found here could help you: How to update one file in a zip archive
If graphical apps are an option, you could use winrar or 7-zip to replace the class. You don't need to extract the jar file to make this work. Just open the jar with one of those apps, go to de directory where is the class file to be replaced, drag-and-drop the new file to replace the old one and save.
Updating class in jar, specify directory?
You need to be explicit. It's perfectly valid to have different classes with the same name in different packages. e.g.
com.example.package.A
com.example.package.subpackage.A
etc...
Updating .JAR's contents from code
A Java JAR file is a normal ZIP file. You can therefore open and modify it with code dealing with ZIPs.
Here's a snippet which works (courtesy of David):
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class JarUpdater {
public static void main(String[] args) {
File[] contents = {new File("F:\\ResourceTest.txt"),
new File("F:\\ResourceTest2.bmp")};
File jarFile = new File("F:\\RepackMe.jar");
try {
updateZipFile(jarFile, contents);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void updateZipFile(File zipFile,
File[] files) throws IOException {
// get a temp file
File tempFile = File.createTempFile(zipFile.getName(), null);
// delete it, otherwise you cannot rename your existing zip to it.
tempFile.delete();
boolean renameOk=zipFile.renameTo(tempFile);
if (!renameOk)
{
throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
}
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
ZipEntry entry = zin.getNextEntry();
while (entry != null) {
String name = entry.getName();
boolean notInFiles = true;
for (File f : files) {
if (f.getName().equals(name)) {
notInFiles = false;
break;
}
}
if (notInFiles) {
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(name));
// Transfer bytes from the ZIP file to the output file
int len;
while ((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
entry = zin.getNextEntry();
}
// Close the streams
zin.close();
// Compress the files
for (int i = 0; i < files.length; i++) {
InputStream in = new FileInputStream(files[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(files[i].getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
tempFile.delete();
}
}
Related Topics
How to Serialize Object to CSV File
Declaring Floats, Why Default Type Double
Why Is System.Arraycopy Native in Java
Is There a Java Equivalent to C#'s 'Yield' Keyword
Block()/Blockfirst()/Blocklast() Are Blocking Error When Calling Bodytomono After Exchange()
When and How Are Classes Garbage Collected in Java
When Not to Use the Static Keyword in Java
When Do You Need to Explicitly Call a Superclass Constructor
How to Run Ssh Commands on Remote System Using Java
Threads Configuration Based on No. of Cpu-Cores
How to Get Java to Ignore the "Trust Store" and Just Accept Whatever Ssl Certificate It Gets
How to Turn Off the Httpsession in Web.Xml
Accurate Sleep for Java on Windows
Retrieve an Image Stored as Blob on a MySQL Db
Spring - Injecting a Dependency into a Servletcontextlistener
How to Do Query Auto-Completion/Suggestions in Lucene
How to Use Selenium Webdriver on Local Webpage (On My Pc) Instead of One Located Somewhere Online