"No Main Manifest Attribute" in ----.jar Netbeans

46,874

Solution 1

Hope there is a problem in your manifest file. Some basic checks might solve your problem.

  • it should under /META-INF/MANIFEST.MF
  • Content should have Main-Class:com.MyCompany.App

If you are using any IDE, there should be an option to export project as runnable jar, you can make use of that to let the IDE take care of correct manifest.

From command line jar cfm filename.jar Manifest.txt com/MyCompany/*.class which generates the Manifest file with following contents

Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: com.MyCompany.App

And then you can run jar command java -jar fileName.jar.

These type of problems are trivial but kills lot of time, just ensure your contents and location of the file is correct.

Solution 2

You need the maven-jar-plugin (see Maven's example). This plugin will create the required entries in the manifest file when the project is built.

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>fully.qualified.MainClass</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

You need the version, otherwise, the project won't build. The fully.qualified.MainClass starts at the package hierarchy.

Solution 3

You could just use this for command line:

java -cp jarFileName.jar full.package.className

You wouldn't have to go into specifics of the Manifest file in this case.

Solution 4

If you look at the properties dialog for the project (from project tab, right click on your project and select properties) you'll see that there is a "run" item in the "Categories" window. Click on it and you'll see a dialog where you can specify the Main Class for the jar. That information will end up in your manifest.

Solution 5

Setting an Entry Point with the JAR Tool:

The 'e' flag (for 'entrypoint') creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file. For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:

jar cfe app.jar MyApp MyApp.class

You can directly invoke this application by running the following command:

java -jar app.jar

If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:

jar cfe Main.jar foo.Main foo/Main.class
Share:
46,874

Related videos on Youtube

A_Elric
Author by

A_Elric

Updated on October 31, 2021

Comments

  • A_Elric
    A_Elric over 2 years

    I recently just started toying around with Maven in java. Time comes to test my project, it works fine in the NetBeans window, running the main class found in App.java (com.MyCompany.App), but when I try to run it from a command line I get an error:

    java -jar fileName.jar
    

    "No Main Manifest Attribute" in fileName.jar

    I have tried adding a manifest.mf file specifying what main is, I've also been into project properties and added it as the main file...

    What's going on?

  • A_Elric
    A_Elric over 11 years
    I gave this a shot, trying java jar cfm testSql-1.0.jar Manifest.txt com/MyCompany/testsql/*.class and it returned the error "Could not find or load main class jar"
  • RP-
    RP- over 11 years
    Do you have an empty line at the end of the manifest file? Last line wont be parsed in a manifest file.
  • A_Elric
    A_Elric over 11 years
    My manifest shows like this: Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: dbell Build-Jdk: 1.7.0_05 It's being generated by the IDE though
  • RP-
    RP- over 11 years
    It seems this is not a runnable jar as you don't have Main-Class attribute. You can try jar cfe filename.jar com.MyComapany.App com/MyCompany/App.class
  • adamdunson
    adamdunson about 11 years
    While yours is a valid option, it doesn't exactly answer the "what's going on?" part of the question.
  • Pete
    Pete almost 11 years
    In Netbeans, Run->Project Configuration->Customize->Run->Main Class->Browse and select you main class. Then re-build.
  • fIwJlxSzApHEZIl
    fIwJlxSzApHEZIl about 10 years
    @adamdunson "What's going on" is directly related to which IDE is involved. For NetBeans specifically, Nate has posted the correct solution above.
  • KFL
    KFL almost 10 years
    @Pete this doesn't work with Maven projects in NetBeans. It only works for projects managed by npproject
  • Socrates
    Socrates over 7 years
    This was the most helpful answer here and should IMO be the main answer of this post.
  • Human
    Human over 4 years
    could you please update this answer? The maven jar plugin has changed..
  • will
    will over 4 years
    @Human ... Thanks that's a good point! The best thing is for YOU readers there, check these points (above) with the latest documentation. Our projects use Gradle now days and I'm not in the loop. Happy to ppoint to a correction if someone wants to post as an Answer here.