Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating file in a jar throws ZipException

Tags:

java

jar

I'm trying to update a file in an existing jar (in this example antlr) using the command:

jar -uf antlrworks-1.2.3.jar org/antlr/codegen/templates/Java/Java.stg 

But I get the following message

java.util.zip.ZipException: duplicate entry: antlr/ANTLRError.class at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:175) at java.util.jar.JarOutputStream.putNextEntry(JarOutputStream.java:92) at sun.tools.jar.Main.update(Main.java:508) at sun.tools.jar.Main.run(Main.java:185) at sun.tools.jar.Main.main(Main.java:1044)

Any ideas?

like image 548
talg Avatar asked Apr 26 '09 07:04

talg


People also ask

How do you update a JAR file?

The Jar tool provides a u option which you can use to update the contents of an existing JAR file by modifying its manifest or by adding files. In this command: The u option indicates that you want to update an existing JAR file.

How do you overwrite a class in a JAR file?

JAR files are just ZIP files. Use a tool like WinZip to extract all the files from the JAR, replace the . class file with yours, recreate the ZIP file using e.g. WinZip, rename it to . jar, overwrite the original JAR file.

How do I change the manifest of a JAR file?

To modify the manifest, you must first prepare a text file containing the information you wish to add to the manifest. You then use the Jar tool's m option to add the information in your file to the manifest. Warning: The text file from which you are creating the manifest must end with a new line or carriage return.


2 Answers

You're trying to do the right thing, but the jar file is problematic - it's got the same entry twice :( (This is legal for a zip file, but not really helpful - and as you can see, it makes jar complain.)

If you run

jar tvf antlrworks-1.2.3.jar > contents

and then look at the generated contents file you'll see that there are various duplicate files. You should probably report this to the antlr project (after checking they don't already know).

As a workaround, you can extract the contents of the jar file, jar it up again, and then you'll have a "working" jar file you can update. (If you only need to do this once, you can just extract, put the file you want in there, and then jar the whole lot up rather than updating it afterwards.)

like image 143
Jon Skeet Avatar answered Nov 05 '22 18:11

Jon Skeet


You can do the same operation with the Ant jar task.

<jar duplicate="preserve" jarfile="...">
   your files
</jar>

the duplicate attribute with the preserve value will take care of the duplicate entries.

As mentioned here, the update attribute with the value “preserve” does tell you that duplicates exist, in this form:

 aPath/aFile already added, skipping

If your file is on top of the list the jar task has to pick tp build itself, your new file will be taken into account.

like image 30
VonC Avatar answered Nov 05 '22 18:11

VonC