jar 文件损坏

发布于 2024-12-06 17:35:29 字数 94 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

拥抱影子 2024-12-13 17:35:29

当您在 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 in MANIFEST.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.

假情假意假温柔 2024-12-13 17:35:29

当您更改 ZIP 的 JAR 扩展名、提取 zip 内容并对文件进行一些修改(例如更改 MANIFEST.MF 文件,这是一种非常常见的情况)时,通常会发生这种情况,很多时候 Eclipse 不会生成 MANIFEST 文件,因为我们想要,或者也许我们想修改它的 CLASS-PATH 或 MAIN-CLASS 值。

当您压缩回文件夹时会出现问题。

有效的 Runnable/Executable JAR 具有以下结构:

myJAR (Main-Directory)
    |-META-INF (Mandatory)
             |-MANIFEST.MF (Mandatory Main-class: com.MainClass)
    |-com 
         |-MainClass.class (must to implement the main method, mandatory)
    |-properties files (optional)
    |-etc (optional)

它的工作并不重要。

如果您的 JAR 符合这些规则,那么如果您使用 ZIP 工具手动构建它,然后将扩展名更改回 .jar,那么 尝试使用以下命令在命令行上执行它:

java -jar myJAR.jar 

当您使用 zip 工具解压、更改文件并再次压缩时,通常 JAR 结构会更改为此结构,这是不正确的,因为在文件系统的顶部添加了另一个目录级别它是一个损坏的文件如下所示:

**myJAR (Main-Directory)
    |-myJAR (creates another directory making the file corrupted)**
          |-META-INF (Mandatory)
                   |-MANIFEST.MF (Mandatory Main-class: com.MainClass)
          |-com 
              |-MainClass.class (must to implement the main method, mandatory)
          |-properties files (optional)
          |-etc (optional)

:)

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:

myJAR (Main-Directory)
    |-META-INF (Mandatory)
             |-MANIFEST.MF (Mandatory Main-class: com.MainClass)
    |-com 
         |-MainClass.class (must to implement the main method, mandatory)
    |-properties files (optional)
    |-etc (optional)

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:

java -jar myJAR.jar 

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:

**myJAR (Main-Directory)
    |-myJAR (creates another directory making the file corrupted)**
          |-META-INF (Mandatory)
                   |-MANIFEST.MF (Mandatory Main-class: com.MainClass)
          |-com 
              |-MainClass.class (must to implement the main method, mandatory)
          |-properties files (optional)
          |-etc (optional)

:)

陪我终i 2024-12-13 17:35:29

可能是因为 MANIFEST.MF 存在问题。如果您知道主类所在的包,请尝试使用以下命令启动主类。

java -cp launcher/target/usergrid-launcher-1.0-SNAPSHOT.jar co.pseudononymous.Server

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.

java -cp launcher/target/usergrid-launcher-1.0-SNAPSHOT.jar co.pseudononymous.Server
暗地喜欢 2024-12-13 17:35:29

问题可能是您的 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.

单身狗的梦 2024-12-13 17:35:29

另外,请确保运行时使用的 java 版本与编译期间使用的 java 版本相同或更高版本

Also, make sure that the java version used at runtime is an equivalent or later version than the java used during compilation

眼眸里的快感 2024-12-13 17:35:29

这是常见问题“manifest”中的错误?是的,这种情况经常发生,这里有一个链接:http: //dev-answers.blogspot.com/2006/07/invalid-or-corrupt-jarfile.html

解决方案:

使用 ant 任务即时创建清单文件,您将获得如下条目:

清单版本:1.0
Ant 版本:Apache Ant 1.6.2
创建者:1.4.2_07-b05(Sun Microsystems Inc.)
主类:com.example.MyMainClass

自己创建清单文件,仅使用基本要素即可解决问题:

清单版本:1.0
主类:com.example.MyMainClass

通过更多的调查,我确信我可以像我知道其他人一样使用 Ant 创建动态元文件 - 我的 ant 版本(1.6.2)、java 版本的组合一定有一些特殊之处(1.4.2_07) 也许还有当前的月相。

注释:

Meta-inf 文件的解析一直是一个问题,已经出现,已修复,然后在 Sun 中再次出现。请参阅:错误 ID:4991229。如果您能确定您(或我)的 Java SE 版本中是否存在此错误,那么您比我更有耐心。

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

Solution:

Using the ant task to create the manifest file on-the-fly gives you and entry like:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.2
Created-By: 1.4.2_07-b05 (Sun Microsystems Inc.)
Main-Class: com.example.MyMainClass

Creating the manifest file myself, with the bare essentials fixes the issue:

Manifest-Version: 1.0
Main-Class: com.example.MyMainClass

With more investigation I'm sure I could have got the dynamic meta-file creation working with Ant as I know other people do - there must be some peculiarity in the combination of my ant version (1.6.2), java version (1.4.2_07) and perhaps the current phase of the moon.

Notes:

Parsing of the Meta-inf file has been an issue that has come-up, been fixed and then come-up again for sun. See: Bug Id: 4991229. If you can work out if this bug exists in the your (or my) version of the Java SE you have more patience that me.

北斗星光 2024-12-13 17:35:29

当我刚刚遇到这个主题时,我想分享一下为什么收到“无效或损坏的 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.

夜空下最亮的亮点 2024-12-13 17:35:29

它也可能是 MANIFEST.MF 中的拼写错误,p.ex。带有两个 : 的构建日期

  Build-Date:: 2017-03-13 16:07:12

It can be a typo int the MANIFEST.MF too, p.ex. Build-Date with two :

  Build-Date:: 2017-03-13 16:07:12
儭儭莪哋寶赑 2024-12-13 17:35:29

尝试使用命令 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.

情痴 2024-12-13 17:35:29

如果 jar 文件末尾有任何额外字节,7-Zip 等浏览器可以打开它,但它将被视为损坏。我使用在线上传系统,该系统会自动在每个 jar 文件的末尾添加一个额外的 LF 字符('\n', 0x0a)。对于此类文件,有多种解决方案来运行该文件:

  • 使用 prayagubd 的方法并将 .jar 指定为命令提示符处的类路径和主类名称
  • 删除文件末尾的额外字节(使用十六进制编辑器或类似 head -c -1 myjar.jar 的命令),并且然后通过双击或正常使用 java -jar myfile.jar 来执行 jar。
  • 将扩展名从 .jar 更改为 .zip,解压缩文件,然后重新创建 .zip 文件,确保 META-INF 文件夹位于顶层。
  • 将 .jar 扩展名更改为 .zip、从 .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:

  • Use prayagubd's approach and specify the .jar as the classpath and name of the main class at the command prompt
  • Remove the extra byte at the end of the file (with a hex-editor or a command like head -c -1 myjar.jar), and then execute the jar by double-clicking or with java -jar myfile.jar as normal.
  • Change the extension from .jar to .zip, extract the files, and recreate the .zip file, being sure that the META-INF folder is in the top-level.
  • Changing the .jar extension to .zip, deleting an uneeded file from within the .jar, and change the extension back to .jar

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.

与君绝 2024-12-13 17:35:29

也许这只是一次侥幸,但当我遇到此错误时,我只需杀死所有在后台运行的 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.

瞳孔里扚悲伤 2024-12-13 17:35:29

当您通过 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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文