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

  • Context: Java 
  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    File
Click For Summary

Discussion Overview

The discussion revolves around the issue of creating an executable JAR file in Java, specifically addressing the "no main manifest attribute" error encountered when attempting to run the JAR. The context includes troubleshooting steps taken using different methods and tools, such as Apache NetBeans and command line operations.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to create an executable JAR file using Apache NetBeans and command line, encountering a "no main class" error despite having a Main-Class attribute in the manifest file.
  • Another participant proposes a solution by suggesting a different command to create the JAR file, which successfully resolves the issue.
  • A third participant reiterates the necessity of defining a main class in the manifest file, suggesting that the default manifest may not have included it.
  • A later reply clarifies that the initial participant had already included the Main-Class attribute, indicating that the problem was not with the manifest content but rather with the command used to create the JAR file.

Areas of Agreement / Disagreement

Participants generally agree that the Main-Class attribute is necessary for the manifest file, but there is disagreement about the cause of the initial error, as one participant believes it was due to the absence of the main method definition, while another points out that the attribute was already present.

Contextual Notes

There are limitations regarding the assumptions made about the default manifest file generated by the tools used, and the specific commands employed to create the JAR file may vary in effectiveness based on the context.

Who May Find This Useful

This discussion may be useful for Java developers encountering issues with creating executable JAR files, particularly those using Apache NetBeans or command line tools.

Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,186
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?
 
Technology news 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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
15K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
1
Views
2K
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
5K