jar 文件损坏
我使用 eclipse 在 Windows 7 中创建了一个 jar 文件。当我尝试打开 jar 文件时,它显示 jar 文件无效或损坏。谁能告诉我为什么 jar 文件无效?
I have created a jar file in windows 7 using eclipse. When I am trying to open the jar file it says invalid or corrupt jar file. Can anyone suggest me why the jar file is invalid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
当您在 Windows 资源管理器中双击 JAR 文件时会发生这种情况,但 JAR 本身实际上并不是可执行 JAR。真正的可执行 JAR 应该至少有一个带有
main()
方法的类,并在MANIFEST.MF
中引用它。在Eclispe中,您需要将项目导出为可运行的JAR文件而不是JAR文件以获得真正的可执行JAR。
或者,如果您的 JAR 只是一堆密切相关的类(库)的容器,那么您不应该双击它,而应该使用某些 ZIP 工具打开它。 Windows 资源管理器默认将 JAR 文件与 java.exe 关联,这不适用于此类库 JAR。
This will happen when you doubleclick a JAR file in Windows explorer, but the JAR is by itself actually not an executable JAR. A real executable JAR should have at least a class with a
main()
method and have it referenced inMANIFEST.MF
.In Eclispe, you need to export the project as Runnable JAR file instead of as JAR file to get a real executable JAR.
Or, if your JAR is solely a container of a bunch of closely related classes (a library), then you shouldn't doubleclick it, but open it using some ZIP tool. Windows explorer namely by default associates JAR files with
java.exe
, which won't work for those kind of libary JARs.当您更改 ZIP 的 JAR 扩展名、提取 zip 内容并对文件进行一些修改(例如更改 MANIFEST.MF 文件,这是一种非常常见的情况)时,通常会发生这种情况,很多时候 Eclipse 不会生成 MANIFEST 文件,因为我们想要,或者也许我们想修改它的 CLASS-PATH 或 MAIN-CLASS 值。
当您压缩回文件夹时会出现问题。
有效的 Runnable/Executable JAR 具有以下结构:
它的工作并不重要。
如果您的 JAR 符合这些规则,那么如果您使用 ZIP 工具手动构建它,然后将扩展名更改回 .jar,那么 尝试使用以下命令在命令行上执行它:
当您使用 zip 工具解压、更改文件并再次压缩时,通常 JAR 结构会更改为此结构,这是不正确的,因为在文件系统的顶部添加了另一个目录级别它是一个损坏的文件如下所示:
:)
This regularly occurs when you change the extension on the JAR for ZIP, extract the zip content and make some modifications on files such as changing the MANIFEST.MF file which is a very common case, many times Eclipse doesn't generate the MANIFEST file as we want, or maybe we would like to modify the CLASS-PATH or the MAIN-CLASS values of it.
The problem occurs when you zip back the folder.
A valid Runnable/Executable JAR has the next structure:
If your JAR complies with these rules it will work doesn't matter if you build it manually by using a ZIP tool and then you changed the extension back to .jar
Once you're done try execute it on the command line using:
When you use a zip tool to unpack, change files and zip again, normally the JAR structure changes to this structure which is incorrect, since another directory level is added on the top of the file system making it a corrupted file as is shown below:
:)
可能是因为
MANIFEST.MF
存在问题。如果您知道主类所在的包,请尝试使用以下命令启动主类。Could be because of issue with
MANIFEST.MF
. Try starting main class with following command if you know the package where main class is located.问题可能是您的 JAR 中的文件超过 65536 个: 为什么 java 抱怨 jar 文件有很多条目? 该问题的 答案。
The problem might be that there are more than 65536 files in your JAR: Why java complains about jar files with lots of entries? The fix is described in this question's answer.
另外,请确保运行时使用的 java 版本与编译期间使用的 java 版本相同或更高版本
Also, make sure that the java version used at runtime is an equivalent or later version than the java used during compilation
这是常见问题“manifest”中的错误?是的,这种情况经常发生,这里有一个链接:http: //dev-answers.blogspot.com/2006/07/invalid-or-corrupt-jarfile.html
This is the common issue with "manifest" in the error? Yes it happens a lot, here's a link: http://dev-answers.blogspot.com/2006/07/invalid-or-corrupt-jarfile.html
当我刚刚遇到这个主题时,我想分享一下为什么收到“无效或损坏的 jarfile”消息的原因和解决方案:
我已将 pom.xml 中的“maven-jar-plugin”版本从 2.1 更新为 3.1 .2.
一切仍然顺利,并且构建了一个 jar 文件。但不知怎的,它显然不会再运行了。
当我再次将“maven-jar-plugin”版本设置回2.1时,问题就消失了。
As I just came across this topic I wanted to share the reason and solution why I got the message "invalid or corrupt jarfile":
I had updated the version of the "maven-jar-plugin" in my pom.xml from 2.1 to 3.1.2.
Everything still went fine and a jar file was built. But somehow it obviously wouldn't run anymore.
As soon as i set the "maven-jar-plugin" version back to 2.1 again, the problem was gone.
它也可能是 MANIFEST.MF 中的拼写错误,p.ex。带有两个
:
的构建日期It can be a typo int the
MANIFEST.MF
too, p.ex. Build-Date with two:
尝试使用命令 jar -xvf fileName.jar,然后将解压文件的内容导出到 Eclipse 中的新 Java 项目中。
Try use the command jar -xvf fileName.jar and then do export the content of the decompressed file into a new Java project into Eclipse.
如果 jar 文件末尾有任何额外字节,7-Zip 等浏览器可以打开它,但它将被视为损坏。我使用在线上传系统,该系统会自动在每个 jar 文件的末尾添加一个额外的 LF 字符('\n', 0x0a)。对于此类文件,有多种解决方案来运行该文件:
head -c -1 myjar.jar
的命令),并且然后通过双击或正常使用java -jar myfile.jar
来执行 jar。所有这些解决方案都要求 .zip 和 META-INF 文件的结构基本正确。它们仅在 zip 末尾使用一个额外字节进行了测试,“损坏”了它。
通过应用
head -c -1 *.jar > 我让自己陷入了真正的混乱tmp.jar
两次。head
插入了 ASCII 文本==> myjar.jar <==
位于文件开头,完全损坏了它。If the jar file has any extra bytes at the end, explorers like 7-Zip can open it, but it will be treated as corrupt. I use an online upload system that automatically adds a single extra LF character ('\n', 0x0a) to the end of every jar file. With such files, there are a variety solutions to run the file:
head -c -1 myjar.jar
), and then execute the jar by double-clicking or withjava -jar myfile.jar
as normal.All of these solutions require that the structure of the .zip and the META-INF file is essentially correct. They have only been tested with a single extra byte at the end of the zip "corrupting" it.
I got myself in a real mess by applying
head -c -1 *.jar > tmp.jar
twice.head
inserted the ASCII text==> myjar.jar <==
at the start of the file, completely corrupting it.也许这只是一次侥幸,但当我遇到此错误时,我只需杀死所有在后台运行的 javaw.exe 进程即可。之后可执行 JAR 就可以工作了。
Maybe this was just a fluke but the one time I had this error, I simply had to kill all javaw.exe processes that were running in the background. The executable JAR worked after that.
当您通过 FTP 将文件传输到目标计算机时,这可能是一件很愚蠢的事情,您去运行 .JAR 文件,但该文件太大,尚未完成传输:) 是的,这发生在我身上。
It can be something so silly as you are transferring the file via FTP to a target machine, you go and run the .JAR file but this was so big that it has not yet been finished transferred :) Yes it happened to me..