JAR FILES

Jar Files


  • Jar files is a bundle of .class files.
  • Once any project development phase is finish then if we want to deploy the project to client or customer phase. For the security purpose,No one deploy the direct java files to client or third party.
  • So the Employer will deploy the .class files in jar format.


    How to make (or) create jar Files:


    • In java there is a command called jar. By using this we can create jar files to .class files.
    • First we should compile the java files and required files should be placed in the same folder.
    • Then from .class file destination, folder invokes the jar command.
    • Syntax :
    •     jar cf jarfilenamedestinationlocation.jar *
        Here jar is command and cf is creating a file where it should created jar file with specified location it can specifies the .class files under classes folder (or) current location.

Example 1:

 
							
                      package com.sdj;
                      public class HelloWorld
                      {
                         public static void main(String[] args) 
                         {
                            System.out.println("Hello World Welcome to SDJ");
                            System.out.println("we are under SDJ Training");
                         }
                      }                            
    • The below diagram shows how to create a jar file for the above program.
    • javapadho
      how to create a jar file
    • The below package structure the jar files creation.
    • javapadho
      package structure the jar files creation

    How To Run Jar Files


    • Syntax :
    •     java -jar fileName.jar
      javapadho
      command line prompt
    • Inside jar file it has bundle of .class files. For running jar file we don’t know which class method will be runned from the bundle of .class files .
    • If we run jar file like below
    •    E:\lab\1stbatch\lib\ java –jar HelloWorld.jar in cmd prompt.
      The error will be
         Failed to load Main-Class manifest attribute from
         HelloWorld.jar.
      Note

        The meaning of error is compiler don’t know where execution starts from the class in that bundle of files i.e. HelloWorld.jar. Because we created jar files without specifying manifest file from HelloWorld.jar

    • To overcome the error, first we need to create text file like ManifestInfo.txt
    • In ManifestInfo.txt again we need to create 3 lines i.e. Manifest-version , createdBy, Main Class .
    • The Manifest-Version & Created-By these two are already created by sun people defaultly. But Main Class should be created explicitly by us i.e.
    • Main-Class: [package name].[class name].


    How to create ManifestInfo.txt :


      javapadho
      ManifestInfo.txt path
    • It should save under src folder.
    • javapadho
      save under src folder


      javapadho
      ManifestInfo.txt
    • the manifestInfo.txt file, com.sdj is package name Pqr is class name .

    For Making Runnable Jar File:


    • Syntax :
    •     jar cfm [jar file name with destination location] [ManifestFileLocation] [input files]
    • We can change order of cfm also like as below.
    • Another way for running :

      • jar cmf [ManifestFileLocation] [jar file name with destination location] [input files]

         Before generating jar files we should compile all .java files.
         We should create ManifestInfo.txt under src.
         Then invoke jar cmd with cmf .

      Example :   for running the above jar file

      javapadho
      jar file location


      javapadho
      jar file with destination location in command prompt


        E:\lab\1stbatch\classes>jar cfm ..\lib\HelloWorld.jar ..\src\ManifestInfo.txt *
        E:\lab\1stbatch\classes> cd ..\lib
        E:\lab\1stbatch\lib>java -jar HelloWorld.jar

        Output

          Hello World Welcome to SDJ
          we are under SDJ Training

    • Another way for Running Runnable jar files:

    •  The below cmd promt will shows the running runnable jar files.
      javapadho
      Running Runnable jar file in command prompt


        E:\lab\1stbatch\classes>jar cmf ..\src\ManifestInfo.txt ..\lib\HelloWorld.jar *
        E:\lab\1stbatch\classes> cd ..\lib
        E:\lab\1stbatch\lib>java -jar HelloWorld.jar

        Output

          Hello World Welcome to sdj
          we are under sdj Training