Today I will show you how to create an executable jar file. I do not know whether this topic is introduced by any other member. Using this method one can build graphical user interface program with java which will behave similar to Executable files i.e., the program can be started with double clicks. It is an easy alternative. Otherwise to run a java program one has to run it through comand prompt(in windows) using java command or by creating java executalbes which are very difficult to make. So lets begin:
Frist of all, we will create a simple java application using java foundation class popularly known as java Swing. Let the file name be JarExample.
CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JarExample {
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
public class JarExample {
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Now compile the java file. JDK will produce two class files namely (a)JarExample.class ( JarExample$1.class.
All the years we have been doing this.Your question might be what new? Its coming.
Now we will create a Manifest File. If you have done java beans then you are quite familiar with the term.
Let write our Manifest File:-
The name of the file is :- jex.mf
CODE
Manifest-Version: 1.0
Main-Class: JarExample
Main-Class: JarExample
We are now ready to create our JAR file.
create a folder, keep the class files (JarExample.class,JarExample$1.class) and the manifest file in that folder.
Now open command prompt. Say for example your folder name is jar and you have kept it in C DRIVE. Now change directory using CD jar command.
Now we will create jar file using following command.
c:\jar>jar cfm jarex.jar jex.mf *.class
It will create jarex.jar .
I wish you will be able to create jar file using this method.
0 comments:
Post a Comment