20

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?

4

6 回答 6

17

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.

于 2014-01-19T23:59:40.927 回答
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.

于 2012-09-04T15:52:07.413 回答
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.

于 2013-03-06T17:58:33.933 回答
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
于 2015-08-11T08:40:51.347 回答
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.

于 2015-12-10T15:05:46.370 回答
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.

于 2014-09-01T04:34:22.173 回答