Can't execute jar- file: “no main manifest attribute”

后端 未结 30 1900
不知归路
不知归路 2020-11-21 22:30

I have installed an application, when I try to run it (it\'s an executable jar) nothing happens. When I run it from the commandline with:

java -jar \

30条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 22:47

    I had the same problem. A lot of the solutions mentioned here didn't give me the whole picture, so I'll try to give you a summary of how to pack jar files from the command line.

    1. If you want to have your .class files in packages, add the package in the beginning of the .java.

      Test.java

      package testpackage;
      
      public class Test
      {
          ...
      }
      
    2. To compile your code with your .class files ending up with the structure given by the package name use:

      javac -d . Test.java
      

      The -d . makes the compiler create the directory structure you want.

    3. When packaging the .jar file, you need to instruct the jar routine on how to pack it. Here we use the option set cvfeP. This is to keep the package structure (option P), specify the entry point so that the manifest file contains meaningful information (option e). Option f lets you specify the file name, option c creates an archive and option v sets the output to verbose. The important things to note here are P and e.

      Then comes the name of the jar we want test.jar.

      Then comes the entry point .

      And then comes -C . / to get the class files from that folder, preserving the folder structure.

      jar cvfeP test.jar testpackage.Test -C . testpackage/
      
    4. Check your .jar file in a zip program. It should have the following structure

      test.jar

      META-INF
      | MANIFEST.MF
      testpackage
      | Test.class
      

      The MANIFEST.MF should contain the following

      Manifest-Version: 1.0
      Created-By:  (Oracle Corporation)
      Main-Class: testpackage.Test
      

      If you edit your manifest by hand be sure to keep the newline at the end otherwise java doesn't recognize it.

    5. Execute your .jar file with

      java -jar test.jar
      

提交回复
热议问题