开发者

Creating an executable jar

开发者 https://www.devze.com 2023-04-04 19:18 出处:网络
I had written a program: public class SystemShutdown { public static void main(String[] args) { try{ for(int i=0;i<10;i++){

I had written a program:

public class SystemShutdown {
    public static void main(String[] args) {
        try{
            for(int i=0;i<10;i++){
                Thread.sleep(1000);
            }
            Process p=Runtime.getRuntime().exec("shutdown -s -t 2700");
        }catch(Exception e){}
    }
}

I'd compiled and kept the .class file separate.

Now, I'd write a manifest file as:

Manifest-Version: 1.0
Main-Class: SystemShutdown

And saved with the name MANIFEST.MF

I'd put both (the .class file and the MANIFEST.MF file) in same directory.

Now I want to create an Executable Jar file. For that I'd done:

jar cvfm MyJar.jar *.*

A jar file is created after that. But wh开发者_开发技巧en I tries to execute it displays a message Java Exception occured.

Can anybody help me out? I want to execute this program on the users double click.

Beside of the above scratch can anybody tell me the exact steps to be followed to create an executable jar?

I'm using Windows7 32bit and jdk7


The m option of the command line for jar says you'll provide the manifest file as the following parameter (in this case, after the jar file itself). So I suspect you want:

jar cvfm MyJar.jar MANIFEST.MF SystemShutdown.class

See the jar tool documentation for more details.

EDIT: I've just tried this and it works fine. Code:

// In Test.java
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

// Manifest in MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: Test

Command line and output:

javac Test.java
jar cvfm test.jar MANIFEST.MF Test.class
java -jar test.jar
Hello

Note that if you don't have a line terminator at the end of the Main-Class line in the manifest, that will cause an error, but it's somewhat better specified:

Failed to load Main-Class manifest attribute from test.jar
0

精彩评论

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

关注公众号