How Do I Make an Executable JAR File

What’s is Jar? it is built on the ZIP file format and have the .jar file extension and introduced to Java to describe Java binary files, it is combination of all classes and corresponding Metadata information.

A magic feature of Java2 is the ability to make a Jar file executable, It is amusing as running it just like to run windows executable program. Assuming your program is Swing GUI, now you can double click the shortcut to quick start your graphic screen.

Creating executable Java class

To make executable jar it is mandatory to make at least one class executable. Java specification stipulated that the unique launch entrance is the main method of class.

import java.awt.*;

public class ExecutableCalss {
    public static void main(String[] args) {
        String title = "Just an example to make your jar file executable";
        Frame frame = new Frame(title);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}

Creating a Manifest file

Manifest.mf references a series of medadata of Jar, you can create or edit Manifest.mf by using any text editors. We have additional chapter: Use Ant Create manifest.mf. In case of this, the two attributes Main-Class and Class-path are required in manifest file to making jar executable. Please create a manifest file as following:

Manifest-Version: 1.0
Main-Class: ExecutableCalss
Class-path: .

Main-Class specifics the launch entrance class. Class-path specifics the run-time class-path.

Making an executable jar file now

We have prepared class files and manifest.mf. Now use Jar command to create a jar file by executing:

jar cvfm executable_jar.jar manifest.mf ExecutableCalss.class

The second parameter “executable_jar.jar” specifics your new created jar name.
The thrid parameter specifics manifest.mf file path.
The fourth parameter specifics your classes path.

We have generated executable_jar.jar, Now please double-click the Jar file to start up, alternatively, you also can use “java -jar executable_jar.jar” to start it.

Оцените статью
ASJAVA.COM
Добавить комментарий

Your email address will not be published. Required fields are marked *

*

code

  1. Bill

    A little less of the java language and a little more the english language!!! Yikes.

    Ответить