aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Got an error message Failed to load Main-class manifest attribute from sampleproject.jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Got an error message Failed to load Main-class manifest attribute from sampleproject.jar" Watch "Got an error message Failed to load Main-class manifest attribute from sampleproject.jar" New topic
Author

Got an error message Failed to load Main-class manifest attribute from sampleproject.jar

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I followed the instruction in Chapter 9 of the Monkhouse book, when I run javaw -jar sampleproject.jar alone, I got this error "Failed to load Main-Class manifest attribute from sampleproject.jar."

I have a DVD directory.
I compile DVD/src/sampleproject and put the classes in DVD/classes/sampleproject
I created Manifest.mf file and specify the main class is in sampleproject.gui.ApplicationRunner
Under DVD directory, I type jar -cfm sampleproject.jar Manifest.mf -C classes .
I created a jar file, sampleproject.jar in DVD directory.

Under DVD directory, I type the javaw -jar sampleproject.jar alone
I got the above error message.

Please advise me.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Could you show the contents of your manifest.mf?

If you have a look into the jar file, is the manifest.mf included?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I don't have the book on hand now.
In the Manifest.mf file, I typed the two lines as told by Monkhouse book. I include sampleproject.gui.ApplicationRunner.
There are just two lines.

I created the sampleproject.jar file. I use jar xf sampleproject.jar to open the file. I only see Meta-Inf folder. Inside it, the Manifest.mf file is the standard one , not the one I created. And I don't see any other folders.
I suggest something wrong with the jar.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

You have to make sure the manifest.mf in the meta-inf directory is the one you created. Otherwise java does not know which class contains the main-method.

If you simply copy some command from Andrew's book, it might not have the desired effect because Andrew's setup of his computer will differ from your's. And location from where he invoked scripts/commands may also be different from your's. Always think about this when just copying something from the book.

Here you'll find a bit more info about generating a jar file with a manifest-file.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Here are the steps I did:
1. Create a Manifest.mf file. In the file I have two lines:
Manifest-Version : 1.0
Main-Class sampleproject.gui.ApplicationRunner
This Manifest file is in c:\users\workspace\DVD directory

2. Under my c:\users\workspace\DVD (where src anc classes folders are), I type jar -cfm sampleproject.jar Manifest.mf -C classes .

3. Under c:\users\workspace\DVD, I type jar xf sampleproject.jar

4. Meta-Inf folder is created, and there is only one file which is Manifest.mf. No other files are there. (Something wrong here, which I don't know why)

5. In this Manifest.mf file, this file is the default one:
Manifest-Version: 1.0
Manifest-Version: 1.0
Created By: 1.6.0 (Sun Microsystem, Inc)

I am sure step 2, the jar command suggested by Monkhouse is correct. But things don't work out for my machine.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

I followed exactly the same steps as described in Andrew's book and it worked for me. I even called my manifest file "abc.txt", so my command to jar was:

jar -cfm sampleproject.jar abc.txt -C classes .
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Here is my file structure:
c:\users\workspace\DVD\src for source code
c:\users\workspace\DVD\classes for class files where classes\sampleproject contains the db, remote, gui... packages.
I created sampleproject.jar under c:\users\workspace\DVD directory.
The Manifest.mf that I created is also in c:\users\workspace\DVD directory.

I download the source code, create a project called DVD in Eclipse. There is a bin in Eclipse to store all the compiled files. But I follow Monkhouse's steps in Chapter 9. I create a classes folder under DVD directory.
I checked all the spelling and the spellings are correct.

I guess my jar executable is not working? Or I should try this jar -cfm sampleproject.jar abc.txt classes/sampleproject ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Helen Ma wrote:I guess my jar executable is not working?

That would be weird. But it's easy to test:

1/ Unzip the attached file
2/ Compile the single class in src directory
3/ Then run jar -cfm sampleproject.jar abc.txt -C classes . (from directory containing "abc.txt" and folders "src" + "classes") and it should make a runnable jar without any problem (using "abc.txt" as Manifest.mf file)

Good luck!


[Download dennysDVDs2.0.zip] Download

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Thanks. I will try to do that at home.

By the way, I am trying a simple experiment in my office computer using netbean platform
I have c:\myProgram\javaApplication\src. I have a simple program called JavaApplication.java
I compile it under my command prompt. So, I have c:\myProgram\javaApplication\classes\JavaApplication.class
Under c:\myProgram\javaApplication, I type jar -cfm sample.jar abc.txt -C classes .
where abc.txt contains "Main-Class: javaApplication.JavaApplication"

So, I typed jar -xf sample.jar sample.jar
In c:\myProgram\javaApplication, I hava javaApplication package and META-INF folder. I opened the META-INF folder, I
see the default manifest.mf, instead of abc.txt

Here is the problem. jar -cfm does not seem to add acb.txt to META-INF folder.
So, when I type javaw -jar sample.jar , I got the same error message.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Helen Ma wrote:where abc.txt contains "Main-Class: javaApplication.JavaApplication"

That's a non-existing class, because based on your directory structure your class is in the default package (instead of the package "javaApplication"
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Sorry for the confusion. I forget to type the javaapplication as the package. I forgot the mention that in the src there is javaApplication\JavaApplication.java.
So, in DVD directory, there is a classes\javaApplication\JavaApplication.class
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

It's very strange. I did exactly the same and there was no problem at all.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I have been trying this with my office computer. I got this fail to load Main-Class manifest attribute from xxx.jar.
In the exam, I think it is required to create Manifest.mf file ourselves. If that won't work on either my machine or office machine, it may not run on the assessor's machine.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
It looks like the contents of the abc.txt cannot be added to the meta-inf/manifest.mf file in the jar.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
After I type the jar cfm sample.jar abc.txt -C classes .
I typed jar -xf sample.jar. A meta-inf folder is generated. I open it and see Manifest.mf is there. I opened Manifest.mf , the Main-Class: .... in abc.txt does not exist.
I have a feeling that jar fails to add this Main-Class to the manifest file.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Can you create a runnable jar through an IDE like Eclipse?

Have a look at the ant-script I made (it's in ScjdFaq) and try to use ant to build the runnable jar.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, Roel.
Thanks for your help.
I found out that the parser that parsed the manifest.mf file is very picky.
In the abc.txt file, after the Main-Class: sampleproject.gui.ApplicationRunner, I need to hit an Enter key to move the cursor to the beginning of the next line. And after Main-Class:, I need to put exactly one space, no more or no less, before sampleproject.

Then, I saved the file and everything works.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

I believe that picky thingy about Enter key is mentioned in the book (or somewhere else), because I remember having read about it yesterday (but don't know anymore where )

Glad to hear you were able to solve it!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10052
    
163

Roel De Nijs wrote:I believe that picky thingy about Enter key is mentioned in the book (or somewhere else),


It's mentioned in the Java Manifest doc too


[My Blog] [JavaRanch Journal]
 
 
subject: Got an error message Failed to load Main-class manifest attribute from sampleproject.jar