What are .jar files ?
The JAR file format is based on the popular ZIP file format. Usually these files are used for archiving and distributing the files and implementing various libraries, components and plug-ins in Java applications.
In Eclipse IDE -> Java Project's, JRE System Library is the collection of .jar files.
Creating .jar file using Eclipse IDE
1. Before creating the .jar files, lets create few our own functions which help us while writing Java Programs in Eclipse IDE.
First function to create -> arunAddsAndPrints( ) -> Takes two values, adds and prints the result in output
Second function to create -> arunSubsAndPrints( ) -> Takes two values, substracts and prints the result in output.
So I am creating my own functions which I thought of using in my Java Programs.
Lets create this functions in Eclipse IDE by following the below steps
2. Launch Eclipse IDE, create Java Project 'Project 016' as shown below -
2. Create two packages 'packageAdds' and 'packageSubs' as shown below -
3. Under package 'packageAdds', create a Java Class 'ClassAdds' with the code for adding values and printing result to output as shown below -
4. Under package 'packageSubs', create a Java Class 'ClassSubs' with the code for subtracting values and printing result to output as shown below -
5. Save all the files in the project and follow the below steps to create .jar file
6. Right Click on the Project and select 'Export' option as shown below -
7. In 'Export' dialog, select 'JAR' option under the 'Java' folder and click on 'Next' button as shown below -
8. In 'JAR Export' dialog, click on 'Browse' button to specify the destination of the JAR file on our computer as shown below -
9. Choose a destination on your computer, give the .jar file name and click on 'Save' button as shown below -
10. Click on 'Finish' button as shown below -
11. Go to the destination where you saved the .jar file as shown below -
This is how we create JAR files using Eclipse IDE.
12. Now lets use the created JAR file in another Java Project by following the below steps
13. Create another Java Project 'Project 017' in Eclipse IDE as shown below -
14. Now lets import the JAR file 'arun.jar' created in step 11 in the new Java Project 'Project 017' and use the function 'arunAddsAndPrints( )' and 'arunSubsAndPrints( )' by following the below steps.
15. Right Click on Project and select 'Properties' option as shown below -
16. In 'Properties' -> Java Build Path -> Libraries tab, select 'Add External JARs' button as shown below -
17. In 'JAR Selection' window, select the earlier created 'arun.jar' file and click on 'Open' button as shown below -
18. Ensure 'arun.jar' file is displayed under the 'Libraries' tab and click on 'OK' button as shown below -
19. Observe that a Referenced Libraries with arun.jar file is displayed in the project as shown below -
20. Now create a Class 'ClassXYZ' as shown below -
21. Now use the functions of the above created arun.jar file (added to this project in step 18) in the ClassXYZ class of Project 017 as shown below -
22. Save and Run the ClassXYZ class and observe that the output is displayed as shown below -
In this way we can create our own JAR files with our own methods, add the created JAR files to other Java Projects and access the functions defined in the JAR files
Please comment below to feedback or ask questions.
Using 'length' array attribute with 2 dimensional arrays will be explained in the next post.