javamanifestexecutable-jar

JAR Command not using supplied Manifest


Unsure why I am getting this error, I feel that I am missing something very simple. This is an issue I am facing trying to follow this post on creating a JAR executable from the command line. Here are my simple test files:

JarExample.java:

public class JarExample {
    public static void main(String[] args) {
        
    }
}

MANIFEST.MF:

Manifest-Version: 1.0
Main-Class: JarExample

I run the follow commands to construct the JAR:

javac JarExample.java
jar cfm JarExample.jar MANIFEST.MF *.class

Yet when I run java -jar JarExample.jar, I get the following error:

no main manifest attribute, in JarExample.jar

Peaking inside the JAR, the correct class file is present, but the following autogenerated manifest, META-INF/MANIFEST.MF, is present:

Manifest-Version: 1.0
Created-By: 1.8.0_162 (Oracle Corporation)

So where is my usage of the jar command incorrect? Why does it not recognize my supplied manifest file?

Thanks!


Solution

  • I have followed all steps that you described, and an executable jar file has been successfully created.

    I'm using openjdk 11.0.11 2021-04-20

    The generated file inside the .jar file looks like this:

    Manifest-Version: 1.0
    Main-Class: JarExample
    Created-By: 11.0.11 (Ubuntu)
    

    So this is may be caused by your Java version or used Operation System.

    In the referenced by you answer, there is a useful comment by David:

    If you create the MANIFEST.MF file, don't forget to finish the last line with a line break. On Windows just add an empty line at the end. Otherwise the last attribute will not make it in the jar file. In this special case the Main-Class attribute will be missing.

    Probably, this is your case. Because when I remove the last line break in the MANIFEST.MF, I'm getting the same error as yours:

    no main manifest attribute, in JarExample.jar

    Example of MANIFEST.MF with the last line break (3rd line is empty) - works well

    Manifest-Version: 1.0
    Main-Class: JarExample
    
    

    Without the last line break (2 lines of code) - crashes with an error

    Manifest-Version: 1.0
    Main-Class: JarExample