java 更新 jar 中的文件会引发 ZipException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/790430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating file in a jar throws ZipException
提问by talg
I'm trying to update a file in an existing jar (in this example antlr) using the command:
我正在尝试使用以下命令更新现有 jar(在本例中为 antlr)中的文件:
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)
java.util.zip.ZipException:重复条目:antlr/ANTLRError.class at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:175) at java.util.jar.JarOutputStream.putNextEntry(JarOutputStream.java:92)在 sun.tools.jar.Main.update(Main.java:508) 在 sun.tools.jar.Main.run(Main.java:185) 在 sun.tools.jar.Main.main(Main.java:1044) )
Any ideas?
有任何想法吗?
采纳答案by Jon Skeet
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 jarcomplain.)
您正在尝试做正确的事情,但是 jar 文件有问题-它有两次相同的条目:((这对于 zip 文件是合法的,但并不是很有帮助-正如您所见,它会引起jar抱怨。)
If you run
如果你跑
jar tvf antlrworks-1.2.3.jar > contents
and then look at the generated contentsfile 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).
然后查看生成的contents文件,你会看到有各种重复的文件。您可能应该将此报告给 antlr 项目(在检查他们还不知道之后)。
As a workaround, you can extract the contents of the jar file, jar it up again, and thenyou'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.)
作为一种解决方法,您可以提取 jar 文件的内容,再次对其进行 jar,然后您将拥有一个可以更新的“工作”jar 文件。(如果你只需要这样做一次,你可以提取出来,把你想要的文件放在那里,然后把整个文件打包,而不是事后更新。)
回答by VonC
You can do the same operation with the Ant jartask.
您可以对Antjar任务执行相同的操作。
<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:
正如这里提到的,值为“preserve”的更新属性确实告诉你存在重复项,形式如下:
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.
如果您的文件在 jar 任务必须选择 tp build 本身的列表顶部,您的新文件将被考虑在内。
回答by Sam Barnum
If you're on OS X, try the Jar Inspectorapplication. 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.
如果您使用的是 OS X,请尝试使用Jar Inspector应用程序。我用它来修补 wicket 中的一个 javascript 错误。您可以打开一个 jar 文件,它会列出所有内容。导航到要保存的文件(在本例中为 .js 文件)并修改该文件,然后保存内容,它会为您修改 .jar 文件。不确定这是否适用于 .java 文件。

