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

  • Java
  • Thread starter Wrichik Basu
  • Start date
  • Tags
    File
In summary, the individual is having trouble creating an executable JAR file for their program, which consists of three classes (Prog, CreateThread, and BuildGUI). After trying to create the JAR file using Apache NetBeans 11.0 and following various tips from StackOverflow, they were still getting an error that there was no main class. They then tried creating the JAR file from the command line and encountered the same error. After researching and trying different methods, they found that adding the Main-Class attribute to the MANIFEST.MF file and using a different set of commands to create the JAR file solved the issue. They are sharing this information for others who may encounter the same problem.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
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:
MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: BuildGUI
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
  • #2
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 sysprog
  • #3
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 sysprog
  • #4
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.
 

1. What does the error message "no main manifest attribute" mean when creating a JAR file?

This error message means that there is no main class specified in the manifest file of the JAR. The main class is the entry point for the JAR file, so without it, the JAR cannot be executed.

2. How do I fix the "no main manifest attribute" error when creating a JAR file?

To fix this error, you need to add the main class to the manifest file of the JAR. This can be done by specifying the main class in the Main-Class attribute of the manifest file. Make sure to use the fully qualified name of the main class, including the package name.

3. Why does the "no main manifest attribute" error occur even though I have specified the main class in the manifest file?

This could be due to a typo in the main class name or an incorrect file path. Double check that you have correctly specified the main class and that the file path is correct.

4. Can I create a JAR file without a main class?

No, a JAR file must have a main class specified in the manifest file in order to be executable. The main class serves as the entry point for the JAR file.

5. How can I prevent the "no main manifest attribute" error when creating a JAR file?

To prevent this error, make sure to specify the main class in the manifest file before creating the JAR. Also, double check that the main class name and file path are correct. It may also be helpful to use a build tool, such as Maven or Gradle, to manage the creation of JAR files and ensure that the manifest file is properly configured.

Similar threads

  • Programming and Computer Science
Replies
2
Views
385
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
1
Views
830
  • Programming and Computer Science
Replies
12
Views
9K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
3
Views
15K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top