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

后端 未结 30 1942
不知归路
不知归路 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:49

    Since you've add MANIFEST.MF, I think you should consider the order of Field in this file. My env is java version "1.8.0_91"

    and my MANIFEST.MF as here

    // MANIFEST.MF
    Manifest-Version: 1.0
    Created-By: 1.8.0_91 (Oracle Corporation)
    Main-Class: HelloWorldSwing
    
    // run
    ~ java -jar HelloWorldSwing.jar
    no main manifest attribute, in HelloWorldSwing.jar
    

    However, this as below run through

    Manifest-Version: 1.0
    Main-Class: HelloWorldSwing
    Created-By: 1.8.0_91 (Oracle Corporation)
    
    //this run swing normally
    
    0 讨论(0)
  • 2020-11-21 22:49

    (first post - so it may not be clean)

    This is my fix for OS X 11.6, Maven-based Netbeans 8.2 program. Up to now my app is 100% Netbeans - no tweaking (just a few shell escapes for the impossible!).

    Having tried most all of the answers here and elsewhere to no avail, I returned to the art of "use what works".

    The top answer here (olivier-refalo thanx) looked like the right place to start but didn't help.

    Looking at other projects which did work, I noticed some minor differences in the manifest lines:

    1. addClasspath, classpathPrefix were absent (deleted them)
    2. mainClass was missing the "com." (used the NB -> Project Properties->Run->Main Class->Browse to specify)

    Not sure why (I am only 3 months into java) or how, but can only say this worked.

    Here is just the modified manifest block used:

        <manifest>
            <mainClass>mypackage.MyClass</mainClass>
        </manifest>
    
    0 讨论(0)
  • 2020-11-21 22:52

    If you are using the command line to assemble .jar it is possible to point to the main without adding Manifest file. Example:

    jar cfve app.jar TheNameOfClassWithMainMethod *.class
    

    (param "e" does that: TheNameOfClassWithMainMethod is a name of the class with the method main() and app.jar - name of executable .jar and *.class - just all classes files to assemble)

    0 讨论(0)
  • 2020-11-21 22:53

    The MAVEN problem is that its try to include the first MANIFEST.MF file from first library from dependencies instead of THE OUR OWN MANIFEST.MF WHEN YOU USE ARTIFACTS!.

    1. Rename yourjar.jar to yourjar.zip
    2. Open MANIFEST.MF file from META-INF\MANIFEST.MF
    3. Copy the real MANIFEST.MF that already generate in your project by MAVEN That include somelike that:

      Manifest-Version: 1.0 Main-Class: yourpacket.yourmainclass (for exmaple info.data.MainClass)

    4. Replace the content of MANIFEST.MF from youjar.zip with it.

    5. Rename yourjar.zip to yourjar.jar back.
    6. Now java -jar yourjar.jar work perfectly.

    OR!

    Simple create you own MANIFEST.MF and:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
            <archive>
                <manifestFile> Your path like: src/main/resources/META-INF/MANIFEST.MF </manifestFile>
                <index>true</index>
                    <manifest>
                        <addClasspath>true</addClasspath>
                    </manifest>
            </archive>
        </configuration>
    </plugin>
    

    But if you use maven panel (or maven command line) you can force it to generate own manifest and include it into JAR file.

    1. Add to the you pom.xml's build section this code:

      <plugins>
          <plugin>
      
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.2.0</version>
          <executions>
              <execution>
                  <phase>package</phase>
                  <goals>
                      <goal>single</goal>
                  </goals>
              </execution>
          </executions>
      
          <configuration>
              <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
      
              <archive>
      
                  <index>true</index>
      
                  <manifest>
                      <addClasspath>true</addClasspath>
                      <mainClass> yourpacket.yourmainclass (for exmaple info.data.MainClass)</mainClass>
                  </manifest>
                  <manifestEntries>
                      <mode>development</mode>
                      <url>${project.url}</url>
                  </manifestEntries>
              </archive>
          </configuration>
      </plugin>
      

    2. Open the MAVEN panel (in Intellij) and execute "Install". It will generate the MANIFEST file and compile property the JAR file with all dependencies into the "Target" folder. Also it will be installed to the local maven repository.

    0 讨论(0)
  • 2020-11-21 22:54

    For maven, this is what solved it (for me, for a Veetle codebase on GitHub):

    <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.lazydevs.veetle.api.VeetleAPI</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
     </plugins>
    </build>
    

    Cheers...

    0 讨论(0)
  • 2020-11-21 22:55

    I had the same issue. by adding following lines to pom file made it work. The plugin will make sure the build process of your application with all necessary steps.

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
    0 讨论(0)
提交回复
热议问题