Let's get started with a Microservice Architecture with Spring Cloud:
Importance of Main Manifest Attribute in a Self-Executing JAR
Last updated: July 29, 2025
1. Overview
Every executable Java class has to contain a main() method. Simply put, this method is the starting point of an application.
To run our main() method from a self-executing JAR file, we’ve to create a proper manifest file and pack it along with our code. However, when we try to run our application using the java -jar command, we may encounter the error:
no main manifest attribute, in example.jar
This error happens when Java can’t find the class with the main() method because the JAR file is missing a required entry in its manifest file.
In this tutorial, we’ll learn how to create a simple self-executing JAR file and understand why the main manifest attribute is essential. We’ll also discuss how to fix this common error and make sure our JAR runs smoothly.
2. Executing a JAR Without the Main Manifest Attribute
To get more practical, we’ll show an example of unsuccessful execution without the proper manifest attribute.
Let’s write a simple Java class with a main method:
public class AppExample {
public static void main(String[] args){
System.out.println("AppExample executed!");
}
}
To pack our example class into a JAR archive, we have to go to the shell of our operating system and compile it:
javac -d . AppExample.java
Then we can pack it into a JAR:
jar cvf example.jar com/baeldung/manifest/AppExample.class
Our example.jar will contain a default manifest file. We can now try to execute the JAR:
java -jar example.jar
Execution will fail with an error:
no main manifest attribute, in example.jar
2.1. Specifying the Main Class
Let’s run a JAR file without a Main-Class attribute in its MANIFEST.MF file. We can use the -cp or -classpath option to ensure that our jar file is in the classpath and then provide our main class in the package.className format:
java -cp example.jar com/baeldung/manifest/AppExample
We can also run:
java -classpath example.jar com/baeldung/manifest/AppExample
This time the JAR executes as expected and outputs:
AppExample executed!
3. Executing a JAR With the Main Manifest Attribute
As we’ve seen, the JVM couldn’t find our main manifest attribute. Because of that, it couldn’t find our main class containing our main method.
Let’s include a proper manifest attribute in the JAR along with our code. We’ll need to create a MANIFEST.MF file containing a single line:
Main-Class: com.baeldung.manifest.AppExample
Our manifest now contains the classpath to our compiled AppExample.class. Since we already compiled our example class, there’s no need to do it again.
We’ll pack it together with our MANIFEST.MF file:
jar cvmf MANIFEST.MF example.jar com/baeldung/manifest/AppExample.class
This time the JAR executes as expected and outputs:
AppExample executed!
4. Common Pitfalls When Creating Executable JARs
Even after including a manifest file, we might still see the “no main manifest attribute” error. This usually means something went wrong with how the manifest was written or how the JAR was packaged. In this section, we’ll look at common mistakes that can cause the error to persist.
4.1. Typo in the Main-Class
One of the most common problems is a typo in the Main-Class declaration. The value must be the fully qualified name of the class that contains the main() method — including the full package path.
For example, if our class is com.example.AppExample, and we write Main-Class: AppExample, Java won’t be able to find it.
4.2. Missing the Blank Line
Another common issue is missing the blank line at the end of the MANIFEST.MF file. The manifest format requires that every entry ends with a new line. If the file doesn’t end properly, Java may ignore the entire file or fail to read it correctly.
4.3. Wrong Placement of the Manifest File
The placement of the manifest file is also important. When we package our JAR, the manifest must be located inside the META-INF directory. If we use the jar command manually and don’t include the m option to specify our custom manifest, Java falls back to a default one, which doesn’t include the Main-Class.
5. Adding the Main-Class Attribute with Build Tools and IDEs
Instead of writing the MANIFEST.MF file manually, we can let our build tools handle it for us. This is a more reliable and scalable way to include the Main-Class attribute, especially in larger projects.
5.1. Using Maven
Maven can automatically generate a manifest file and include the Main-Class. In our pom.xml, we simply add maven-jar-plugin with the proper configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.AppExample</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Once this is added, we run:
mvn package
Maven builds a JAR in the target/ directory with the Main-Class already set in the manifest.
5.2. Using Gradle
If we’re using Gradle, we can add the Main-Class attribute inside the jar task in build.gradle:
jar {
manifest {
attributes(
'Main-Class': 'com.example.AppExample'
)
}
}
Then we run the build:
gradle build
The executable JAR is generated in build/libs/.
5.3. Using Ant
For Ant, we define the Main-Class attribute inside the manifest tag when building the JAR:
<target name="jar" depends="compile">
<jar destfile="dist/example.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="com.example.AppExample"/>
</manifest>
</jar>
</target>
Running this task produces a JAR with the correct MANIFEST.MF.
5.4. Configuring Main-Class in IDEs (IntelliJ & Eclipse)
Both IntelliJ and Eclipse provide built-in ways to set the Main-Class.
IntelliJ makes it easy to set the main class for a JAR by simply following the steps below:
- Go to File > Project Structure
- Click Artifacts > + > JAR > From modules with dependencies
- Select the module and main class
- Make sure “Extract to the target JAR” is selected
- Build the artifact from the Build > Build Artifacts menu
This generates a JAR with the Main-Class set correctly.
For Eclipse, we follow these steps:
- Right-click the project > Export
- Choose Runnable JAR file
- Select the launch configuration (the main class)
- Choose the destination JAR file and library handling option
- Click Finish
Eclipse automatically generates a MANIFEST.MF with the Main-Class.
6. Conclusion
In this quick article, we learned how to pack a simple Java class as a self-executing JAR, and we demonstrated the importance of a main manifest attribute on two simple examples. We also explored how to fix the common “no main manifest attribute” error, which often occurs when the manifest is missing, incorrect, or improperly formatted.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
















