What is JAR file?
JAR stands for the Java ARchive. Before discussing on JAR files let us discuss about ZIP file first. A ZIP file is file format which can store one or more files in the compressed format. ZIP files are most commonly used formats. JAR is also a compressed file format which normally contains the java class files (.class files) and a manifest file (metadata). Sometimes it may contain java source code files (.java files), images etc. For better understanding JAR files are ends with the .jar extension.
How to create JAR file in eclipse?
Let us discuss it with an example. We are creating a java project which contain one class ArmstrongNumber.java. The ArmstrongNumber.java contains one static method armstrong(int num) which returns true if the num is an armstrong number otherwise returns false. We will create the JAR file of this and use JAR file in other project.
Step 1: Right click on the project and click on Export.
Step 2: Select the JAR file and click on Next.
Step 3: Enter the destination and click on Finish.
Now the JAR is created on the destination folder.
Test the created jar:
We are creating a new java project ArmstrongJarTest which contains one class ArmstrongTest.java. Add the ArmstrongJar.jar into the project class path and then use the armstrong(int num) method to check whether the no. is armstrong or not.
Example:
ArmstrongTest.java
import com.w3schools.business.ArmstrongNumber; /** * This class is used to test the custom jar. * @author w3schools */ public class ArmstrongTest { public static void main(String args[]){ //Use the static method from the jar file to check whether the //no. is armstrong or not. boolean isArmStrong = ArmstrongNumber.armstrong(407); if(isArmStrong){ System.out.println("Given number is an armstrong number."); }else{ System.out.println("Given number is not an armstrong number."); } } } |
Output:
Given number is an armstrong number. |
Related Topics: