Saturday 8 March 2014

How to create and extract JAR (Java ARchive) file



                                     How amazing!!! 
              When all your class files run only on a double click!
Let’s see the magic

Step – 1
                For creating JAR file you must install latest java SE (Standard Edition).
            -Here I installed JDK 1.7.
            -There are two type application of JAR file
1.      GUI base application.
2.      Command base application.

Step -2
·         Create the CMD base application.
Here firstly I show the command base application.
FirstPro.java
import java.io.*;
class Demo
{
   void display()
    {
        System.out.println("Hi I am ASHUTOSH  !!!!");      
    }
}
class FirstPro
{
  public static void main(String args[])
     {
           Demo d=new Demo();
           d.display();
   }
}
Compile this program and check is it correct or not?
Step-3
·         Create manifest file
                This file inform to JVM start execution form which (main()) class.
            There are many attribute available for that fie but we need use only two.
-          This file should save with .mft extension.
-          Manifest-version:  version of file.
-          Main class: execution from this class
-          Should not give any space after writing main-class name
-          Must press twice time enter after giving main-class name. 
-           Write following code in notepad and save with .mft extention. 
        Here I created xyz.mft. 
          you can chang this name .
Xyz.mft
__________________________________________________________________________________
Manifest-version: 1.0
Main-class: FirstPro


__________________________________________________________________________________
·         Those file should save in same directory.

Step-4
·         Create jar file.

·         Syntax of JAR file.
jar  [options] [manifest] destination file name [additional input file ]

·         There are many options available for creating JAR file but I used only 4.
Options are :-
C - It creates anew or empty archive on the standard output.
F – The argument following this option specified a JAR file to work with.
V – It generates “verbose” output on stderr.
M – It includes manifest information from a specified manifest file.




·         Use this command for create JAR file.
jar cvmf xyz.mft Ashu.jar *.class”.


·         Run Jar file

Use this command for run JAR file.
java -jar Ashu.jar


·         Table view of JAR file
Use this command for table view.

jar tf Ashu.jar
 

Step -5
·         Create a GUI base application.
            Now we are creating GUI base application.
·         Again do the same thing for creating GUI base application.
Step – I
Firstly write a program.
Save as a MyFrame.java
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class MyFrame
{
    public static void main(String[] args)
     {
        JFrame myFrame = new JFrame("My First Frame");
        myFrame.setVisible(true);
        myFrame.setBounds(300, 300,300,300);
        JLabel myText = new JLabel("Hi I am ASHUTOSH",
        SwingConstants.CENTER);
        myText.setForeground(Color.red);
        myFrame.getContentPane().add(myText, BorderLayout.CENTER);
        myFrame.addWindowListener(new MyClass());
    }
}




class MyClass extends WindowAdapter
{
     public void windowClosing(WindowEvent e)
      {
                  System.exit(0);
      }
}
______________________________________________________________________________
Compile this program and check is it correct or not?

        Step – II
·          create Manifest file.
 Save as a xyz.mft

Manifest-version: 1.0
Main-class: MyFrame
                                                                                        



Step – III
·         Create a JAR file using this command.
jar cvmf xyz.mft Ashu.jar *.class

Step – VI
·         Run a JAR file using this command.
“java -jar Ashu.jar”
Or
Double click on your JAR file.






·         If you have done all stapes correctly although your JAR file not running then do following stapes.
-          Right click on JAR file.
-          Go to “Open with->choose default program->brows
-          Select path “C:\Program Files\Java\jdk1.7.0_51\bin\javaw”.
-          Now your file ready to run.

·         The basic difference between CMD base application and GUI base application is CMD base App run only using command and
GUI base application run either command or simple double clicking on JAR file.



·         You can unzipped JAR file using “WinRar archiver” software.



Enjoy the JAVA world



                                 -Created By Ashutosh
(Indiraites)
For any grievance mail me on
      Ashutosh-ashujagtap333@gmail.com



No comments:

Post a Comment