Jar File Tutorial

What Is Jar ?


1. Jar stand for java archive is a packaging tool that is used to contain group of packages which contains group of classes.

2. There are two way to create a jar file:

  1. Non - Runnable Jar
  2. Runnable Jar

Basically Java is used to develop two type of things:

  1. API Development
  2. Application Development

1. API Development

API stand for Application Programming Interface which contains classes that contains services (methods) to perform certain task.

This is non executable file because some other programmers want to use your services then you need to distribute an API like Payment Gateway, Google Map API etc.

2. Application Development

Application Development is executable file which is used by client like notepad, calculator etc.

For Example :

Suppose programmer A create an weather API used by programmer B and programmer B wants to create a weather application for client.

So here A produce API and B consume API to create an Application either Desktop Based, Web Based, Android Based etc.

i) Non-Runnable Jar :

  • It is used for API development.
  • To create a non-runnable jar you need to follow below command:
    jar cvf yourjarname.jar rootpackage/*
  • where c : create v : verbose f : file

ii) Runnable Jar :

  • It is used for Application development.
  • To create a runnable jar you need to follow below command:
    jar cfe yourjarname.jar fullyqualifiedmainclassname rootpackage/*
  • where c : create f : file e : executable

How to extract a Jar file

  • To extract a jar file you need to follow command:
    Jar xvf yourjarfile.jar
  • where x : extract v : verbose f : file

1. Non-Runnable Jar Example

Create an API to add two numbers and produce a result like below:

        package com.deepsingh44.blogspot;

        public class Cal {

         // this is my add service or method
         public int add(int num1, int num2) {
          return num1 + num2;
         }

    }

Save file : Cal.java

Compile file : javac -d . Cal.java


Consume Cal API like below:

        package com.example;
        import com.deepsingh44.blogspot.Cal;
        import java.util.Scanner;
        public class Test {

         public static void main(String [] args){
                Cal cal=new Cal();
                Scanner scan=new Scanner(System.in);

                System.out.println("Enter First Number : ");
                int num1=scan.nextInt();
                System.out.println("Enter Second Number : ");
                int num2=scan.nextInt();
                
                //call the service from here
                int result=cal.add(num1,num2);
                
                System.out.println("Output is : " + result);
         }

    }

Save file : Test.java

Compile file : javac -d . Test.java

Run class : java com.example.Test

Note : When you compile Test class you get compile time error because compiler not found com.deepsingh44.blogspot.Cal pcakage class then you need to set classpath location like below:

set classpath=.;./cal.jar;

1. Runnable Jar Example

Create a Simple Application to show alert message using Runnable Jar just like below:

        package com.deepsingh44.blogspot;
        import javax.swing.JOptionPane;
        public class Application{

            public static void main(String [] args){
                JOptionPane.showMessage(null,"Hello Runnable Example");
            }
        }
    

Save file : Application.java

Compile file : javac -d . Application.java

jar cfe app.jar com.deepsingh44.blogspot.Application com/*

Then double click on app.jar file and execute.

No comments: