Introduction: getting to know the error that scares beginners
You wrote a program in Java, compiled it, packaged it into a JAR file, and proudly try to run it with a double click — but instead of the application window, you see a black terminal window with a frightening message: Can't execute jar- file: "no main manifest attribute". Sound familiar? If not — consider yourself lucky (for now).
This error is one of the most popular on Stack Overflow. Just imagine: thousands of developers, from students to experienced engineers, have encountered at least once in their lives that their JAR file "refuses" to run. Why does this happen? The thing is, Java is not just "package and run." The virtual machine must clearly know: which class contains the entry point? Without this information, the JAR file remains just an archive, not an executable program.
In this article, we will figure out what a manifest is, how it relates to the entry point, and — most importantly — how to make your JAR file run without errors. We will go from simple to complex: from manual assembly in the command line to IDE configuration. Let's go!
What is a manifest and why is it needed?
Every JAR file (Java Archive) contains a META-INF folder inside, and inside it — a MANIFEST.MF file. This is not just a technical artifact, but a real "instruction manual" for the Java virtual machine.
Imagine a book without a table of contents. You know it has chapters, but you don't know which page to start reading from. Approximately the same thing happens with a JAR file without specifying the main class. The virtual machine sees many classes, but doesn't know which one contains the public static void main(String[] args) method.
A typical MANIFEST.MF file looks like this:
Manifest-Version: 1.0Created-By: 17.0.2 (Oracle Corporation)Main-Class: com.example.MainThe most important line here is Main-Class. It specifies the full class name (with package) that Java should run. If this line is missing or specified incorrectly — you get that very "no main manifest attribute" error.
Why can the manifest be empty or incorrect?
- Manual assembly via
jar— if you didn't specify the-eflag or didn't create your own manifest. - Building in an IDE without configuration — some development environments (especially older versions) do not add
Main-Classautomatically. - Oversight in naming — for example, you wrote
mainwith a lowercase letter or forgot to specify the package. - Encoding and formatting — every empty line at the end of the file is important in the manifest.
How to create an executable JAR file: three ways
Method 1. Command line (manual control)
This method gives a complete understanding of the process. Let's say we have a simple class HelloWorld.java:
package com.example;
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world! JAR is working!"); }}Step 1. Compile the class
javac -d . HelloWorld.javaThe -d . flag creates the package structure (com/example/HelloWorld.class).
Step 2. Create the manifest manually
Create a file MANIFEST.MF (for example, in Notepad) with the following content:
Manifest-Version: 1.0Main-Class: com.example.HelloWorldImportant: there must be an empty line after the last line! Without it, the JAR file may not read the manifest.
Step 3. Create the JAR
jar cfm HelloWorld.jar MANIFEST.MF com/example/HelloWorld.classThe
Blogs
Book Recommendations
Read also