What is JAR in Java?
JAR stands for Java Archive. It is a packaging tool used to contain a group of packages which in turn contain groups of classes. JAR is used to distribute your code as a single file.
What is JAR?
- JAR stands for Java Archive, a packaging tool that contains group of packages which contains group of classes.
- There are two ways to create a JAR file:
- Non-Runnable JAR
- Runnable JAR
Java is used to develop two types of things:
- API Development
- Application Development
1. API Development
API stands for Application Programming Interface which contains classes that contain services (methods) to perform certain tasks.
This is a non-executable file because if other programmers want to use your services, you need to distribute an API like Payment Gateway, Google Map API etc.
2. Application Development
Application Development is an executable file used by the client like Notepad, Calculator etc.
Example: Programmer A creates a weather API used by programmer B, and programmer B creates a weather application for the client. So A produces API and B consumes API to create Desktop, Web, Android application.
i) Non-Runnable JAR
- It is used for API development.
- Command to create:
jar cvf yourjarname.jar rootpackage/* - Where c = create, v = verbose, f = file
ii) Runnable JAR
- It is used for Application development.
- Command:
jar cfe yourjarname.jar fullyqualifiedmainclassname rootpackage/* - Where c = create, f = file, e = executable
How to Extract a JAR File
- Command:
jar xvf yourjarfile.jar - Where x = extract, v = verbose, f = file
1. Non-Runnable JAR Example - Create API to Add Two Numbers
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: javac -d . Cal.java
Create JAR: jar cvf cal.jar com/*
Consume Cal API:
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: Test.java | Compile: javac -d . Test.java | Run: java com.example.Test
set classpath=.;./cal.jar;
2. Runnable JAR Example - Simple Alert Application
package com.deepsingh44.blogspot;
import javax.swing.JOptionPane;
public class Application{
public static void main(String[] args){
JOptionPane.showMessageDialog(null,"Hello Runnable Example");
}
}
Save: Application.java | Compile: javac -d . Application.java
Create Runnable JAR: jar cfe app.jar com.deepsingh44.blogspot.Application com/*
Then double click on app.jar file and execute.
Conclusion
Use Non-Runnable JAR when you want to distribute an API for other developers. Use Runnable JAR when you want to give an executable application to the client.
No comments:
Post a Comment