tags:

views:

1451

answers:

3

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?

+6  A: 

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.)

Jon Skeet
+1  A: 

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.

VonC
A: 

If you're on OS X, try the Jar Inspector application. I used it to patch a javascript bug in wicket. You can open a jar file, and it lists all the contents. Navigate to the file you want to save (in this case, a .js file) and modify the file, then save the contents, and it takes care of modifying the .jar file for you. Not sure if this would work with .java files or not.

Sam Barnum