views:

601

answers:

3

I have these files board.class and x4.class (x4.class has main() method).

To jar these files, I wrote

jar cf x4.jar *.class

and got a x4.jar file.

I copied this x4.jar file to my Desktop (on windows Vista) and double-clicked it. I am getting this error:

Failed to load Main-Class manifest attribute from C:\Users\eSKay\Desktop\x4.jar

What should I do to make this file run as a jar executable (without installing any software)?


UPDATE: I used a manifest file to fix the problem. I have got the jar file I needeed and it is running fine if you do:

java -jar x4.jar

But, when I double click x4.jar nothing happens, I checked Task Manager and found that a javaw.exe is being started in the background, but it is not showing the output the original program was giving.

What can the problem be?

A: 

Get a Mac, then.

alex tingle
+4  A: 

You need to create a manifest file which contains the Main-Class attribute to specify its entry point. Then use the "m" flag in the jar command to specify it. For example, you might have a file called manifest.txt:

Manifest-Version: 1.0 
Main-Class: x4

Note that you need to have an empty line at the end of the file, or the jar tool won't process it properly, ignoring the final line silently.

Then run:

jar cfm x4.jar manifest.txt *.class

To test it, run:

java -jar x4.jar
Jon Skeet
i tried it, still exactly the same error!
Lazer
If you've specified the attribute correctly you won't get *exactly* that error. Please post the contents of your manifest file. Run "jar tvf x4.jar" to make sure that the jar file really contains the manifest - it'll be there as `meta-inf/MANIFEST.MF` IIRC.
Jon Skeet
@Jon earlier I did not create any manifest file. After you told me, I just copy pasted those two lines to my manifest.txt file. "jar tvf x4.jar" shows that the manifest file is present. a screenshot - http://imgur.com/7YHAS.jpg . Its in front of me, somehow I am getting the "exactly same" error!!
Lazer
Actually, I think you get a META-INF/MANIFEST.MF file no matter what. But if you use the "m" parameter, then you can *override* the default one. So, @eSKay, can you try extracting the JAR file and making sure the manifest is the one you want?
Matt Solnit
@Matt you are right, i unjarred it and the Manifest file is not what i provided. What am I doing wrong? My manifest file (unjarred version) reads "Manifest-Version: 1.0 Created-By: 1.6.0 (Sun Microsystems Inc.)"
Lazer
@eSKay -- Make sure you have a newline or carriage return following the Main-Class: x4 line. I knew @Jon was right, but wanted to see if there were any quirks. At 1st I was getting the same error. But then I remembered you needed a CR/LF. I found the following statement:"Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return. "here: http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html
Wayne Young
Wayne's right - without the newline at the end, the Main-Class bit will be removed. I'll edit the answer to make this clearer.
Jon Skeet
I just checked, carriage return is also there. somehow, the manifest file that is being included in x4.jar is the default one, not the one I am providing.
Lazer
And you're using exactly the command line I provided? I've just tried it and it was fine after I'd got the extra line terminator in there.
Jon Skeet
yes, i was missing the line terminator, it showed okay in Notepad++ but was not there when I opened the file through vi (using SSH to a linux machine). So I added it and now the error is gone. But, when I double click x4.jar nothing happens, I checked Task Manager and found that a javaw.exe is being started in the background, but it is not showing the output the original program was giving.
Lazer
What happens when you run "java -jar x4.jar" from the command line?
Jon Skeet
@Jon it runs fine that way. what might the problem be?
Lazer
A: 

I think @Jon is correct, just make sure you end the file with a CR/LF.

Setting an Application's Entry Point

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

Or you can let the jar program automatically create the Main-Class attribute for you.

The 'e' flag (for 'entrypoint'), introduced in JDK 6, creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a jar file. Use it to specify the application entry point without editing or creating the manifest file. For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:

jar cfe app.jar MyApp MyApp.class

You can directly invoke this application by running the following command:

java -jar app.jar

If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:

jar cfe Main.jar foo.Main foo/Main.class
Wayne Young
(same comment as posted on Jon's post) yes, i was missing the line terminator, it showed okay in Notepad++ but was not there when I opened the file through vi (using SSH to a linux machine). So I added it and now the error is gone. But, when I double click x4.jar nothing happens, I checked Task Manager and found that a javaw.exe is being started in the background, but it is not showing the output the original program was giving.
Lazer