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?
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.
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.
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.
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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With