Question is how to create executable jar file in java. We want to paste jar file in desktop and it should run with double click or we can run jar file using java command in console.
Here are steps to create jar file :
1. Create Java file to execute : Here is sample example example of disposable AWT Frame example to complete this exercise.
Java file Name : AWTFrame.java
import java.awt.*; import java.awt.event.*; class AWTFrame extends Frame { AWTFrame() { this.setTitle("Example of Executable jar through this window"); this.setSize(400,400); this.show(); this.addWindowListener( new WindowAdapter() { public void windowClosing (WindowEvent event) { System.exit (0); } } ); } public static void main(String args[]) { AWTFrame obj = new AWTFrame(); } }
2. Compile Java file to create class file : We need to compile this java file to get class file
javac AWTFrame.java
3. Create Meta File : Meta file need to be created. This will provide details of class from which execution need to be started.
File Name : AWTFrame.mf
Manifest-Version: 1.0 Main-Class: AWTFrame
4. create execute jar file :
Command to create executable jar is given below.
Command to create executable jar is given below.
jar cmf AWTFrame.mf AWTFrame.jar AWTFrame*.class
We can have n number of classes in jar file and it will be utilized by class file.
5. Test Executable jar file by double click in desktop and using java utility in console :
Use following command to run from console :
java -jar AWTFrame.jar
Or just copy into desktop and double click.
6. Output of the jar file :
0 comments:
Post a Comment