1

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!

Gumpf
  • 197
  • 7
  • 1
    It’s hard to retrace your issue as whatever the problem causes is likely not present in our systems or may unconsciously be avoided by us. You can use the `v` option to get more diagnostic output. Note that for just setting the main class, you don’t need to deal with the manifest at all. You can use `jar cfe JarExample.jar JarExample *.class` to set the **e**ntry point aka main class to `JarExample`. And use `jar vcfe JarExample.jar JarExample *.class` to get the **v**erbose output. – Holger Dec 10 '21 at 13:53
  • This link should help -> [link](https://docs.oracle.com/javase/tutorial/deployment/jar/build.html) – Ruthvik Dec 10 '21 at 14:16

1 Answers1

2

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
VadymVL
  • 5,366
  • 3
  • 26
  • 41