Creating an Executable JAR
You have developed a Swing/Frame Application,and you want to give it to your client.The Application contains too many classes,and the client has no knowledge of executing the java program.
In such scenario, you would be interested to create an Executable jar,and then handle it to the client.The client will simply double click the executable jar,and the program starts running.He is not bothered about how to execute the java program.
Lets see how to create the executable jar file
Directory Structure
LoginPage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| package p1;import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPasswordField;import javax.swing.JTextField;public class LoginPage { public LoginPage() { JFrame frame = new JFrame(); JLabel usernameLbl = new JLabel("UserName"); JLabel passwordLbl = new JLabel("Password"); JTextField usernameTf = new JTextField(10); JPasswordField passwordTf = new JPasswordField(10); JButton loginBtn = new JButton("Login"); JButton registerBtn = new JButton("Register"); // add actionListener to your button,and write the logic for button // click frame.setLayout(new FlowLayout()); frame.add(usernameLbl); frame.add(usernameTf); frame.add(passwordLbl); frame.add(passwordTf); frame.add(loginBtn); frame.add(registerBtn); frame.setSize(250, 300); frame.setVisible(true); }}//class |
MyMainClass.java
1
2
3
4
5
6
7
8
9
10
| package p1; public class MyMainClass { public static void main(String[] args) { LoginPage loginPage=new LoginPage(); } }//class |
mymanifest.txt
Main-Class: p1.MyMainClass
Note:-Please press enter after writing the first line in manifest file.The manifest file should be terminated with a new line,else you will get parsing error
After Developing all the resources,compile your java classes
c:\ExecutableJars\p1>javac *.java (If you have explicitly created the folder p1 and kept all your java file there
c:\ExecutableJars>javac -d . *.java If you have All java files inside ExecutableJars folder,and you want to automatically generate the package folder(p1)
Create the Executable Jar.
While creating the executable jar,you need to mention manifest file as well.A manifest file is the file that contains main-class name,so that when ever the executable jar is clicked,the main class will be loaded for execution.
Execute the following command to create executable jar
c:\executableJar>jar cvfm LoginApp.jar mymanifest.txt p1/*.class
c -stands for create the jar
v -verbrose(diplay the processing details)
f – create the jar file with the given name(here LoginApp)
m -manifest file,we are providing manifest file(mymanifest.txt)
p1/*.class include all .class file present in p1 folder
v -verbrose(diplay the processing details)
f – create the jar file with the given name(here LoginApp)
m -manifest file,we are providing manifest file(mymanifest.txt)
p1/*.class include all .class file present in p1 folder
If your application uses jpeg,gif,you would also like to include them while creating your executable jar file
After Executing the above command,you will get LoginApp.jar file in your current directory.Just double click the file,and you can see the application running.
Hope this Application proved to be a helpful one.


No comments:
Post a Comment