Explain Codes LogoExplain Codes Logo

Can't execute jar- file: "no main manifest attribute"

java
manifest
maven
ant
Anton ShumikhinbyAnton Shumikhin·Nov 1, 2024
TLDR

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:
      <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>VERSION</version> <configuration> <archive> <manifest> <mainClass>com.yourpackage.YourMainClass</mainClass> <!-- Cool main class entrance! --> </manifest> </archive> </configuration> </plugin>
  • In Ant:
    • Spell out Main-Class with <manifest> within the <jar> task:
      <jar destfile="dist/YourApp.jar"> <manifest> <attribute name="Main-Class" value="com.yourpackage.YourMainClass"/> </manifest> ... </jar>
  • In Gradle:
    • Under jar block of build.gradle notate this:
      jar { manifest { attributes 'Main-Class': 'com.yourpackage.YourMainClass' // Here we go, main class! } ... }

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:

<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>

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 the maven-assembly-plugin:
    <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <archive> <manifest> <mainClass>com.yourpackage.YourMainClass</mainClass> <!-- Your main class to the rescue --> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> <!-- We got you covered --> </descriptorRefs> </configuration> </execution> </executions> </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.