Creating a Jar file in Java
The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the class' main() method.
You must produce an a.mf file, also known as a manifest file, in order to create an executable jar file.
A manifest file is created
We must type Main-Class, a colon, a space, and the class name, then enter to create a manifest file. For instance:
myfile.mf
Main-Class: Abc
The mf file begins with the Main-Class colon gap class name, as you can see. Here, CJFE is the class name.
Just after the class name in the mf file, a new line is required.
using the jar tool to create an executable jar file
The jar tool offers a variety of switches, some of which are listed below:
- -c generates a fresh archive file.
- The output that is verbose is produced using the -v option. The extracted or included resource is shown on the standard output.
- The -m option includes manifest data from the specified mf file.
- -f specifies the name of the archive file.
- The -x option extracts files from the archive file.
Let's now write the code necessary to create an executable jar from a mf file.
You must type jar, switches, mf file, jar file, and then as shown in the .classfile:
jar -cvf MyJarFile.jar Abc.class
for running or executing the class file which is in the far file we should use this command.
java -cp MyJarFile.jar Abc
We are presuming that you have used AWT or SWING to develop any window-based applications. If not, you can just use the following code:
Abc.java
import javax.swing.*;
public class Abc{
Abc(){
JFrame f=new JFrame();
JButton b=new JButton("Click Here");
b.setBounds(130,100,100, 40);
f.add(b);
f.setSize(300,400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Abc();
}
}
Output:

This is what the jar file looks like:

If we double, click on the myjar file then it will display as shown below.
