views:

100

answers:

3

Hi all,

I have the following Java code. It does what it is meant to do, but I am having problems creating a jar file.

import java.io.*;
public class openfile{
    public static void main(String argv[]) {
        try {
                String line;
                Process p = Runtime.getRuntime().exec
                ("c:\\Users\\user\\Desktop\\"+ "shares.bat /A");
                 BufferedReader input =
                 new BufferedReader
                 (new InputStreamReader(p.getInputStream()));
                 input.close();
                }
           catch (Exception err) {
                  err.printStackTrace();
               }
    }
}

It compiles no problem, It runs no problem when using java openfile. The problem arises when I try to create a jar file using the following commands:

  jar cf MyJar.jar manifest.txt openfile.java openfile.class

However when I try to run the jar using

java -jar MyJar.jar

I get the following error message:

Failed to load Main-Class manifest attribute from MyJar.jar

The text of manifest.txt is as follows:

Main-Class: openfile 

Any idea what I am doing wrong?

+2  A: 

I got the problem i believe.

In your manifest.txt file

The text of manifest.txt is as follows:

Main-Class: openfile 

You need to provide a line break after last line

So just type Enter (<-) (carriage return) after this line.

Main-Class: openfile 

Refer to this sun documentation. here is an excerpt.

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.

I verified with this and it works. Hope this helps .

JWhiz
Hi,Thanks for responding. Tried this still getting the same error. I started from scratch. Deleted the old manifest.txt, typed the line in a new file and hit the Enter button (so that cursor is on a new line), saved the file and created new jar file. Made no difference.
Anthony Keane
I think u have not used **`jar cfm MyJar.jar manifest.txt openfile.class openfile.java`**u should use **m** option, or else it will provide a default manifest file without class definition. I rechecked again.. i worked perfect.. u r missing the **m** option
JWhiz
A: 

Assuming, you have a class openfile with a main method declared, then this should work:

jar cvef MyJar.jar openfile openfile.class

If you already have a manifest and want to include it, then try this:

jar cvmf MyJar.jar manifest.txt openfile.class
Andreas_D
Hi,Tried both versions of this answer, to no avail.
Anthony Keane
A: 

The Content of your Manifest File must be this:

Manifest-Version: 1.0
Main-Class: openfile

A blank line must be left after these two lines as not leaving it might create problem.

Logan
Hi,Tried this and still having the same problems. If you look at the two answers above it does not seem to want to work. Is it possible a configuration problem on my system?
Anthony Keane
Try extracting the file and see the content. The content must be:1) Meta-Inf Folder containing manifest.txt file2) And otherfile.class file (outside meta-inf folder)If that is there, it should work.
Logan
HI Logan, Extracted the jar file and it contains the openfile.class file and a META-INF folder that contains a MANIFEST file containing the following (over 3 lines): Manifest-Version: 1.0 Created-By: 1.6.0_21 (Sun Microsystems Inc.) Main-Class: openfileYet I am still getting the same error. Is it possible there is something wrong with my java configuration?
Anthony Keane
I am not sure about java configuration. Remove the line created by........... and make sure there is a blank line after main-class line. If that blank line will not be there, the jar will not work. ( By blank line I mean, go to line after Main-Class and press enter once). Wish u luck.
Logan
Also, my Manifest files extension is .MF, try changing it, if it works, let me know.
Logan