开发者

Trouble Creating an executable Java Jar file

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 manife开发者_如何学编程st.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?


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 .


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜