Wednesday, April 30, 2014

In Java, Jar file Creation manual


Jar file Definition:

The JAR file format is a compressed format used primarily to distribute Java applications and libraries. It is built on the ZIP file format, and functions in a similar way; many files are compressed and packaged together in a single file, making it easy to distribute the files over a network. If you need to package a Java application or library, you can create a JAR file using the Java Development Kit (JDK) and your computer's command prompt.

Steps for Creating Jar File:
Step1:
First Create a simple java file:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JFrameDemo {

            public static void main(String s[]) {
                        JFrame frame = new JFrame("JFrame Source Demo");
                        // Add a window listner for close button
                        frame.addWindowListener(new WindowAdapter() {

                                    public void windowClosing(WindowEvent e) {
                                                System.exit(0);
                                    }
                        });
                        // This is an empty content area in the frame
                        JLabel jlbempty = new JLabel("");
                        jlbempty.setPreferredSize(new Dimension(175, 100));
                        frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
                        frame.pack();
                        frame.setVisible(true);
            }
}
Step2:
Compile this file:
javac JframeDemo.java
Step3:
Create a manifest file & save as manifest.txt
Content of the file given below:


Main-Class: JframeDemo

Step4:
Create the JAR file using this code:
jar cfm JframeDemo.jar Manifest.txt JFrameDemo.class
Step5:
Now Click on the jar file to execute directly





No comments:

Post a Comment