Wednesday, May 17, 2023

How to fix Failed to load Main-Class manifest attribute from jar in Java? Eclipse Netbeans Tutorial Example

If you have tried creating JAR file and running Java program from command the line you may have encountered "Failed to load Main-Class manifest attribute from jar", This error which haunts many Java programmer when they enter into command line arena and tries to create JAR file in the command line or IDE like Netbeans and Eclipse. The major problem with "Failed to load Main-Class manifest attribute from jar" error is that, it’s unnecessary creates panic which it doesn't deserve, maybe because the process involves of creating and running JAR is not that common like running Java program using the "java" command.


Anyway in this Java tutorial we will see Why  "Failed to load Main-Class manifest attribute from the jar" comes and How to fix  Failed to load Main-Class manifest attribute from jar error.



Cause of Failed to load Main-Class manifest attribute from the jar in Java

As I have said previously, in order to troubleshoot any error first look at the error message, many times it provides important clue though I agree on some the time it also confuses or misleads mainly in case of those NoClassDefFoundError and ClassNotFoundException

Here the error message is relevant and clear "Failed to load Main-Class manifest attribute from the jar" is saying that java command  failed to load "Main-Class" attribute from Manifest of JAR file, which you are running, it means:



1) Either you have not provided "Main-Class" attribute in Manifest file
2) You don't have a manifest file in your JAR
3) "Main-Class" attribute is not spelled correctly

"Main-Class" attribute in MANIFEST.MF file specifies the program entry point or name of Main class, a class that contains the main method in Java. The main method is required to run Java program. 

If your jar's MANIFEST file doesn't contain "Main-Class" than you can not run Java program using the command "java -jar name.jar" , it will result in "Failed to load Main-Class manifest attribute from jar"

In next section we will see step by step examples of how to reproduce this error and then how to fix Failed to load Main-Class manifest attribute from the jar.




Example of "Failed to load Main-Class manifest attribute from the jar":

Failed to load Main-Class manifest attribute from jar - Eclipse Java Netbeans fixIn order to create a JAR file from the command prompt, you need a MANIFEST.MF file which must follow rules specified for manifest here http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html . One of the important rules is "Manifest file must end with newline or carriage return character". Once you have your manifest file ready, you are ready to create the JAR file. 

In this example, we will first create a JAR file whose MANIFEST file doesn't contain Main-Class attribute and then will see how it runs.

here is our MANIFEST.MF file:
user@Machine:~/java cat MANIFEST.MF
Manifest-version: 1.0

There is no Main-class attribute and here is command to create JAR with Manifest file:
user@Machine:~/java jar -cvfm test.jar MANIFEST.MF HelloWorld.class

-c for create
-v for verbose
-f for files
-m for manifest

Above command will create a JAR file named test.jar with MANIFEST file specified after test.jar and include HelloWorld.class from same directory.

When you run this JAR using java command you will get Failed to load Main-Class manifest attribute from jar because our Manifest file doesn't contain "Main-Class" entry:

user@Machine:~/java java -jar test.jar
Failed to load Main-Class manifest attribute from test.jar

Java will also throw Failed to load Main-Class manifest attribute from test.jar  even if you have Main-Class attribute but not spelled correctly so watch out for spelling mistake there.

How to fix Failed to load Main-Class manifest attribute from jar in Java? Netbeans Tutorial Example



How to fix Failed to load Main-Class manifest attribute from jar

Now we know what is causing this error, we can fix it by introducing "Main-Class" attribute in manifest file or correcting it in case of any spelling mistake on "Main-Class". Also remember space between  attribute and its value after colon e.g. "Main-Class:HelloWorld" will not work instead it will throw IOException while creating JAR file as shown here:

user@Machine:~/java cat MANIFEST.MF
Manifest-version: 1.0
Main-Class:HelloWorld

user@Machine:~/java jar -cvfm test.jar MANIFEST.MF HelloWorld.class
java.io.IOException: invalid header field
        at java.util.jar.Attributes.read(Attributes.java:393)
        at java.util.jar.Manifest.read(Manifest.java:182)
        at java.util.jar.Manifest.<init>(Manifest.java:52)
        at sun.tools.jar.Main.run(Main.java:151)
        at sun.tools.jar.Main.main(Main.java:1149)

Once you have correct MANIFEST.MF just repeat the process of creating JAR and running it. This time it should not throw any Error. Here are command examples of running Java program from JAR file :

user@Machine:~/java cat MANIFEST.MF
Manifest-version: 1.0
Main-Class: HelloWorld

user@Machine:~/java jar -cvfm test.jar MANIFEST.MF HelloWorld.class
added manifest
adding: HelloWorld.class(in = 450) (out= 311)(deflated 30%)

user@Machine:~/java java -jar test.jar
Executing Java Program from JAR file

This was the way to fix Failed to load Main-Class manifest attribute from jar error, when you are creating a JAR file from the command prompt but if you are creating JAR from Eclipse or Netbeans IDE, then you need to follow IDE specific steps. Thankfully both IDE asks you to choose Main Class while creating a JAR.

That's all on How to fix "Failed to load Main-Class manifest attribute from jar". We have seen what can cause "Failed to load Main-Class manifest attribute from jar" error and how we can correct it. let me know if you have seen this error due to any other reason than incorrect "Main-class" attribute entry in the manifest file.


Other Java debugging tutorials from Javarevisited Blog

6 comments :

Anonymous said...

Do you get this error, even when you create JAR from Eclipse itself?

javin paul said...

@Anonymous, no you don't get this error when you create executable JAR from Eclipse, because it ask you to choose class with main class explicitly but for normal JAR e.g. library JAR, you do get this error.

Anonymous said...

thanks buddy..you save me.

Sanjay Niyogi said...

Have downloaded nixnote from web which is a nixnote.jar type.I cannot findout what the mail class name is,Its MANUFEST.MF contains Manifest-version: 1.0.Now which classname I should pick ? so as to follow this procedure of making a jar thus adding main class etc

Anonymous said...

a bash script that you can point at offending JAR files would be even nicer.

MR_x said...

I got the exact solution that I need; i.e. the "newline at the end is must in the manifest file". Thanks a lot

Post a Comment