"No Main Manifest Attribute" in ----.jar Netbeans
Asked Answered
M

7

21

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?

Midsummer answered 4/9, 2012 at 15:29 Comment(1)
Check this solution. It's work for me. https://mcmap.net/q/45314/-can-39-t-execute-jar-file-quot-no-main-manifest-attribute-quotTabu
I
10

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.

Ileenileitis answered 4/9, 2012 at 15:52 Comment(6)
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"Midsummer
Do you have an empty line at the end of the manifest file? Last line wont be parsed in a manifest file.Ileenileitis
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 thoughMidsummer
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.classIleenileitis
In Netbeans, Run->Project Configuration->Customize->Run->Main Class->Browse and select you main class. Then re-build.Girhiny
@Girhiny this doesn't work with Maven projects in NetBeans. It only works for projects managed by npprojectRacquelracquet
C
19

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.

Cucurbit answered 19/1, 2014 at 23:59 Comment(1)
This was the most helpful answer here and should IMO be the main answer of this post.Bolide
I
10

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.

Ileenileitis answered 4/9, 2012 at 15:52 Comment(6)
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"Midsummer
Do you have an empty line at the end of the manifest file? Last line wont be parsed in a manifest file.Ileenileitis
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 thoughMidsummer
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.classIleenileitis
In Netbeans, Run->Project Configuration->Customize->Run->Main Class->Browse and select you main class. Then re-build.Girhiny
@Girhiny this doesn't work with Maven projects in NetBeans. It only works for projects managed by npprojectRacquelracquet
M
4

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.

Meath answered 6/3, 2013 at 17:58 Comment(2)
While yours is a valid option, it doesn't exactly answer the "what's going on?" part of the question.Lilith
@adamdunson "What's going on" is directly related to which IDE is involved. For NetBeans specifically, Nate has posted the correct solution above.Rothschild
H
1

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
Huff answered 11/8, 2015 at 8:40 Comment(0)
M
1

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.

Malvin answered 10/12, 2015 at 15:5 Comment(0)
O
0

I have been having this problem with Netbeans 8.0 and the built-in Maven project for the "Java Application" project prototype. Also I have Maven 3 and I found some of the suggestions on the web don't match the maven code used with Netbeans as well.

Anyway here's a simple recipe for having JAR file to run the main-class and embed dependent libraries. I made this work by comparing other project POM files for projects I found that worked with sub-project JAR-s so if someone with better Maven knowledge spots a gottcha, please speak. Also, I left in some normal stuff to provide context. Example follows:

     <properties>
         <packageName>trials.example</packageName>
         <mainClass>${packageName}.CmdApp</mainClass>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

         <slf4jVersion>1.7.7</slf4jVersion>
         <log4jVersion>1.2.17</log4jVersion>

         <maven.compiler.source>1.8</maven.compiler.source>
         <maven.compiler.target>1.8</maven.compiler.target>
     </properties>

This section tells Maven about the project.

  • packageName ... Is the Java package for the main-class
  • mainClass ..... The fully qualified name for class with main() method.

You will see these used in the maven-jar-plugin.

The other thing the built-in example prototype didn't do was to package my sub-projects into the JAR so I can run from the command line. This is done with the maven-dependency-plugin below. The interesting bit is in the where we don't need to package the system stuff, and we want the dependant classes wrapped into our JAR.

These are used as follows:

     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-dependency-plugin</artifactId>
                 <version>2.6</version>
                 <executions>
                     <execution>
                         <id>unpack-dependencies</id>
                         <phase>package</phase>
                         <goals>
                             <goal>unpack-dependencies</goal>
                         </goals>
                         <configuration>
                             <excludeScope>system</excludeScope>
                             <outputDirectory>${project.build.directory}/classes</outputDirectory>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>

             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-jar-plugin</artifactId>
                 <version>2.5</version>
                 <configuration>
                     <useDefaultManifestFile>true</useDefaultManifestFile>
                     <archive>
                         <manifest>
                             <mainClass>${mainClass}</mainClass>
                             <packageName>${packageName}</packageName>
                             <addClasspath>true</addClasspath>
                             <classpathPrefix>classes/</classpathPrefix>
                         </manifest>
                         <manifestEntries>
                            <mode>development</mode>
                            <url>${pom.url}</url>
                         </manifestEntries>
                     </archive>
                 </configuration>
             </plugin>

          </plugins>
    </build>

Hope that saves you the few hours of checking and testing to make it happen. Cheers, Will.

Orvil answered 1/9, 2014 at 4:34 Comment(2)
could you please update this answer? The maven jar plugin has changed..Spada
@Spada ... 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.Orvil
I
0

Maybe your project is under the category of Java with Maven. If you are just building a regular java application I suggest selecting Java with Ant then under the Projects select Java Application. To test, try to add a jFrame in your project package then select Clean and Build the project this will generate folders including dist with the jar file of your project and you will be able to run it without any issues.

Insulting answered 16/4 at 4:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.