Can't execute jar- file 'no main manifest attribute'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When attempting to execute a JAR (Java ARchive) file using the java -jar command, you may encounter the error message "no main manifest attribute". This error indicates that the JAR file does not have the necessary information in its manifest file to identify the class which contains the main method to be executed. Understanding this error involves delving into the structure of JAR files and the roles of manifest files within them.
What is a JAR File?
A JAR file is essentially a package containing a collection of Java classes, associated metadata, and resources. These files are used for distributing and deploying Java applications, libraries, and components. JAR files are built on the ZIP file format and typically have a .jar file extension.
The Role of the Manifest File
A manifest file in a JAR file is a special file that contains information about the files packaged in the JAR file, and crucial metadata to define system settings. The manifest is stored within a directory named META-INF that resides at the root of the JAR.
The most critical attribute in the context of the "no main manifest attribute" error is Main-Class. This main-class attribute is used to specify the entry point of the application, i.e., the class with the main method. When a JAR file is executed, the Java Runtime Environment (JRE) looks for this attribute to determine which class to invoke.
Manifest Example
Here's an example of how a simple manifest file might look:
In this example, Main-Class tells the JRE that the main method to be executed is in the MainApp class within the com.example package.
Common Causes for "No Main Manifest Attribute"
- Missing Main-Class Attribute: The most direct cause is simply that the JAR was created without specifying this attribute.
- Improper Specification: The
Main-Classattribute exists but contains typos or does not match with any class in the JAR. - Incorrectly Built JAR: Errors during the JAR file building process can lead to an improper manifest.
- Using Libraries as Executables: Attempting to run a JAR file meant as a library. Libraries do not usually contain a
Main-Classbecause they are not meant to be standalone executables.
How to Fix This Error
To solve this error, you need to ensure that your JAR file includes a correct Main-Class attribute in its manifest. Below are the methods typically used to include this attribute:
Using the command line:
When creating a JAR from the terminal or command prompt, you can specify the entry point by using the -e switch with the jar tool:
This command creates a JAR named app.jar with the entry point com.example.MainApp.
Using Maven: If you are using Maven, add the following configuration to your pom.xml:
Using Gradle:
For Gradle, update your build.gradle with a similar snippet:
Summary Table
| Issue | Description | Solution |
| Missing Main-Class | Main-Class is not defined in manifest. | Add Main-Class attribute in the manifest. |
| Typo or Incorrect Class Name | Class name in Main-Class is wrong. | Correct the class name in Main-Class entry. |
| General Manifest Errors | Manifest is formatted improperly. | Ensure proper format and entries in manifest. |
| Non-executable JAR | JAR is a library, not an application. | Use the library in an application's context. |
By understanding the role of the manifest file and ensuring that it is correctly configured with the Main-Class attribute, developers can resolve or avoid the "no main manifest attribute" error when executing Java applications packaged as JAR files.

