Problem in creating JAR file - "no main manifest attribute"

  • Context: Java 
  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    File
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 7K views
Messages
2,188
Reaction score
2,694
I have three classes: Prog, CreateThread and BuildGUI. The last class has the main() method. I tested these classes using JDK 12 on NetBeans 11.0, and my program is working fine.

But I am being unable to create an executable JAR file. As you might be knowing, Apache NetBeans 11.0 no longer allows a simple Java application; instead, you must create an application with Maven, Ant or Gradle. I tried using the first, and followed several tricks from StackOverflow, but none of them worked. When I executed the JAR file from command line, it gave me an error, "No main class".

So I switched to creating the JAR file from command line itself. I put all of the three classes into one folder, and compiled them there. I removed the package statements for simplicity (earlier it was something like com.basuLabs.xyz). I followed the answers here to some extent, and wrote down a Manifest file:
[CODE title="MANIFEST.MF"]Manifest-Version: 1.0
Main-Class: BuildGUI[/CODE]
Then I create a JAR file using jar cfm myjar.jar MANIFEST.MF *.class, and run it using java -jar myjar.jar And I get irritated when I get the same error again and again:
no main manifest attribute, in myjar.jar

I can clearly see a Main-Class attribute in Manifest file. What is going wrong here? How can I share my application?
 
on Phys.org
Solution:

Just now I found the solution. I created the JAR file using a different set of commands:
jar cvfe myjar.jar BuildGUI *.class

This is working fine.

I am not requesting a thread deletion, because this seems to be a recurrent problem, and someone else might benefit from this solution.

Help taken from:
http://www.skylit.com/javamethods/faqs/createjar.html
 
  • Like
Likes   Reactions: sysprog
No main manifest error occurs because you did not define any starting point(i.e main method) for the application BuildGUI while creating the executable jar.

The MANIFEST.MF file that was generated by default did not contain Main-Class property in this case.

You just need to add the following line in MANIFEST.MF
Main-Class: BuildGUI

Hope this helps.

Source:
https://javahungry.blogspot.com/2019/05/solved-no-main-manifest-attribute-in-jar.html
 
  • Like
Likes   Reactions: sysprog
Abraham Stones said:
You just need to add the following line in MANIFEST.MF
Main-Class: BuildGUI
As I have written in the first post, I already added a main class attribute in the file. Even then it did not work. Changing the set of commands used to create the jar file, as indicated in post #2, did the trick.