Corrupt jar file

后端 未结 12 1067
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 03:42

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 in

相关标签:
12条回答
  • 2020-11-30 04:24

    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.

    0 讨论(0)
  • 2020-11-30 04:26

    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)
    

    :)

    0 讨论(0)
  • 2020-11-30 04:26

    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.

    0 讨论(0)
  • 2020-11-30 04:26

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

      Build-Date:: 2017-03-13 16:07:12
    
    0 讨论(0)
  • 2020-11-30 04:26

    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.

    0 讨论(0)
  • 2020-11-30 04:30

    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
    
    0 讨论(0)
提交回复
热议问题