You’ve built a Java project and packaged a .jar file.

You are ready to run it outside your IDE, or deploy it somewhere.

When you run it using java -jar <your.jar> it says something like this:

no main manifest attribute, in /src/project/target/your-jar-1.0-SNAPSHOT.jar

How to add a Manifest file

You need to create a META-INF/MANIFEST.MF file with the following content:

Main-Class: com.mypackage.MyClass

The class name com.mypackage.MyClass is the main class that has the public static void main(String[] args) entry point in your application.

If you’re using Maven

If using Maven, then you will need to add this to your POM.xml:

<build>
  <plugins>
    <plugin>
      <!-- Build an executable JAR -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.1.0</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.mypackage.MyClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

If you’re using Gradle

Likewise, if you’re using Gradle, then use this:

plugins {
    id 'java'
}

jar {
    manifest {
        attributes (
                'Main-Class': 'com.mypackage.MyClass'
        )
    }
}

If you didn’t build an Executable Jar

Then you can run it like this:

java -cp app.jar com.mypackage.MyClass

If all else fails: Use this Maven Single Goal

If all else has failed, then you can always use this Maven goal to get the job done:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>com.mypackage.MyClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>