Can't execute jar- file: "no main manifest attribute"
The error "no main manifest attribute" points out to the absence of a Main-Class entry in the jar manifest. Resolve it by injecting a MANIFEST.MF
with:
Main-Class: YourMainClass
Next up, repack your JAR with:
jar cfm YourApp.jar MANIFEST.MF -C classes/ .
Where YourMainClass
is your gateway class, and classes/
is your .class
files' container. Execute it using java -jar YourApp.jar
.
Comprehensive problem explanation and multiple solutions
Unpacking the Manifest mystery
In a JAR file, the MANIFEST.MF
file serves as the mission statement, detailing the main class to kick off the execution flow. Without this, the Java runtime wouldn't know where to hop aboard.
Sloving this in different scenarios
- In Maven:
- Configure the maven-jar-plugin under project's
pom.xml
like this:
- Configure the maven-jar-plugin under project's
- In Ant:
- Spell out Main-Class with
<manifest>
within the<jar>
task:
- Spell out Main-Class with
- In Gradle:
- Under
jar
block ofbuild.gradle
notate this:
- Under
Evading common battle scars
- Skipping the classpath definition or incorrect path setting.
- Absence of signature
public static void main(String[] args)
in the main class. - Misstep of building a JAR without the necessary dependencies. When in doubt, put your faith in Maven's
maven-assembly-plugin
or Gradle's application plugin to get a fat JAR.
Operating Non-Executable Jars
If your JAR isn't constructed to be an executable, it's still possible to run a class within:
java -cp YourLib.jar com.yourpackage.SomeClass
Handling Spring Boot case
Spring Boot necessitates a dedicated plugin to craft an executable JAR:
Unveiling hidden facts with inspection
Tools such as WinRAR or jar tf can unmask the contents of your JAR's MANIFEST.MF:
jar tf YourApp.jar META-INF/MANIFEST.MF
Relying on IDEs
- Eclipse: Opt for the Export feature and then pick your main class.
- IntelliJ IDEA: Apply the Artifacts feature to specify the main class and construct the JAR.
To make your manifest a masterpiece
Crafting Executable Jars with Maven Assembly Plugin
- In your
pom.xml
, slip in themaven-assembly-plugin
: - Confirm that your assembly descriptor includes the 'jar-with-dependencies' format for a comprehensive set of dependencies.
Quick rescue from faulty situations
- Are you using the latest version of build tools and plugins?
- Have you rechecked the classpath configuration and main class package declaration?
- If employing modules, cross-verify module-info.java and
Main-Class
are consistent.
Tips for the road ahead
- Double check the manifesto: If you modify or generate the manifest through tools, validate its format and presence of
Main-Class
. - Lean on your IDE: Many IDEs offer GUI-based JAR export with effortless manifest control.
- Use bird's eye view with CLI: Utilize
jar tvf
to swiftly vet the manifest from the command line. - For the brave hearts: Tinker with shaded or uber JARs for complex dependency management.
Was this article helpful?