Saturday, January 19, 2013

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

If you have tried creating JAR file and running Java program form command 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 command line or IDE like Netbeans and Eclipse. 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, may be because process involves of creating and running JAR is not that common like running Java program using "java" command. Anyway in this Java tutorial we will see Why  "Failed to load Main-Class manifest attribute from 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 jar in Java

Failed to load Main-Class manifest attribute from jar - Eclipse Java Netbeans fixAs 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 some time it also confuse or mislead mainly in case of those NoClassDefFoundError and ClassNotFoundException. Here error message is relevant and clear "Failed to load Main-Class manifest attribute from 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 specify program entry point or name of Main class, a class which contains main method in Java. 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 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 example of how to reproduce this error and than how to fix Failed to load Main-Class manifest attribute from jar.

Example of "Failed to load Main-Class manifest attribute from jar":
In order to create a JAR file from 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 rule is "Manifest file must end with new line or carriage return character". Once you have your manifest file ready, you are ready to create 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

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 is 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 JAR file from command prompt but if you are creating JAR from Eclipse or Netbeans IDE, than you need to follow IDE specific steps. Thankfully both IDE ask you to choose Main Class while creating 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 manifest file.

Other Java debugging tutorials from Javarevisited Blog

1 comment :

Anonymous said...

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

Post a Comment