For working professionals
For fresh graduates
More
Step by Step Java Tutorial Con…
1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
Now Reading
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
In the world of Java programming, JAR files play a significant role in packaging and distributing Java applications. They are a convenient way to bundle multiple Java class files, resources, and libraries into a single archive. If you’re wondering what is JAR full form in Java, the acronym stands for Java ARchive.
In this comprehensive guide, you will learn everything from creating and viewing JAR files to extracting, updating, and running them, as well as how to use JAR files in Java. We will also throw some light on what is JAR file in Java with examples.
Let us begin with the fundamental question: What is a JAR file in Java?
A JAR file is essentially a compressed archive that contains the compiled Java class files, associated metadata, and resources required by a Java application or library. It simplifies the process of distributing Java programs by providing a single file that encapsulates all the necessary components.
To create a JAR file in Java, you can use the jar command-line tool that comes bundled with the Java Development Kit (JDK). Let's take a look at an example of creating a JAR file:
Here is the Java command for producing a JAR file:
jar cvf jar-filename file-input
Let's examine the aforementioned command in more depth. The letter c signifies that we are attempting to generate a jar file, v stands for verbose (which provides more information in the logs than the standard logs); it displays certain command details on the console; and f designates to store the command's output in the file designated in the command prompt.
The command output is saved in a JAR file with the name jar-filename. The path or filename of the file that needs to be compressed and added to the JAR file is the file input.
jar cvf SampleJarFile.jar SampleJavaClass.class
The SampleJavaClass.class file will be compressed using the code above and stored in SampleJarFile.jar.
If the command prompt is open in the current directory, we can specify the file name directly; otherwise, we must provide an absolute path.
In this instance, we are taking into account the command prompt that is open in the current directory.
Java offers a mechanism to view the contents of a JAR file, much like how a file system displays the contents of a folder.
The JAR file's contents are displayed when the following command is run.
jar tf jar-filename
Let's examine the command in more detail. The t suggests that the jar file's contents should be displayed in tabular format, and the f says that the command prompt should contain the jar file's path and filename. Additionally, the command's jar-filename demands the filename of the jar file whose contents are to be displayed.
Here’s an example:
jar tf SampleJarFile.jar
The above code will display the SampleJarFile.jar's contents in a table-like format.
To extract the contents of a JAR file in Java, you can use the jar command or any file archiving tool. Here's an example of extracting a JAR file using the jar command:
jar xf jar-filename
Let's examine the command in greater detail. The letters x indicate that we are attempting to extract the jar file, and the letters f indicate that the jar filename is given in the command prompt itself.
The files we want to extract can be specified explicitly.
jar xf SampleJarFile.jar SampleJavaClass.class
Only the SampleJavaClass.class will be extracted from the SampleJarFile.jar by the aforementioned command.
We could also extract the entire JAR file.
jar xf SampleJarFile.jar
The SampleJarFile.jar's files will all be extracted using the previously mentioned command.
Without specifying the input files using the 'c' option, the jar tool does not permit the generation of a JAR file. However, if a JAR's program files are explicitly removed, the extracted folder of that file will just contain the manifest file that is created by default.
To run a Java application packaged in a JAR file, you can use the Java command followed by the JAR file name.
Here's an example of running a JAR file:
java -cp jar-filename MainClassName
The -cp in the aforementioned command indicates the classpath, which will run the designated class file. The jar tool will not raise a main manifest attribute error without the -cp parameter.
Consider that SampleJavaClass is the main class in our scenario and that the command to run our jar file is
java -cp SampleJarFile.jar SampleJavaClass
The SampleJavaClass.class file will be run using the aforementioned command. Can a JAR be run without using the -cp command?
An executable JAR file contains a special entry in its manifest file that specifies the class containing the main method. This allows the JAR file to be executed directly.
To create an executable JAR file, you need to specify the Main-Class attribute in the manifest file. Here's an example of generating a manifest file and creating an executable JAR:
Manifest-Version: 1.0
Main-Class: com.example.Main
Let's write a straightforward Java swing program.
Since the result of a swing program is displayed on UI, we have written a swing application to test the execution of the JAR file when double-clicking on the file icon.
A Java program example is shown below:
The code must now be compiled using the following command in order to produce the.class file.
The code will be compiled using the aforementioned command, producing the SimpleSwingExample.class file.
Now, let's write the details of the primary class name into a text file. Let's give it the name manifest.txt. This is the text that must be included in the file.
After this step, we'll have a fresh manifest.txt file. The Main-Class property shouldn't be the last line in the file to prevent incorrect processing. At the conclusion, include an empty line.
Let's construct the JAR file now that the manifest has been made. To accomplish this, we must mention the name of this text file in the command line while making the jar file. Here is the code to accomplish it.
We can also build an executable JAR file by including the MainClass name in the command when creating the JAR file. This is the command to carry it out.
The entry point of the JAR file is indicated by option e in the aforementioned command.
Run the following command to run the aforementioned jar files.
Alternatively, you can double-click the JAR file icon, which some operating systems like Windows support. The following GUI will be displayed after running the JAR file.
JAR files are not limited to just Java applications. They can also be used to package Java applets, Java libraries, and even non-Java resources. By including all the necessary files and resources in a single JAR file, you can simplify the deployment process.
To open and extract the contents of a JAR file on various platforms, several JAR file opener tools are available, such as 7-Zip, WinRAR, and Java's JAR command-line tool.
These tools allow you to explore the contents of JAR files and extract specific files or directories.
The JAR command is a powerful tool for working with JAR files. It provides various options for creating, updating, extracting, and viewing JAR files. You can use it from the command line or incorporate it into build scripts to automate the packaging process.
Manifest files play a crucial role in JAR files, as they contain metadata about the JAR file and its contents. They can specify the main class, define class-path entries for dependent JAR files, and provide other information required for the proper execution of the JAR file.
If you are using Maven as your build tool, creating a JAR file becomes even simpler. Maven provides a plugin called "maven-jar-plugin" that automates the process of creating JAR files. By configuring the plugin in your project's POM (Project Object Model) file, you can generate JAR files with ease.
In Java, package sealing allows you to restrict access to classes and resources within a specific package. By sealing a package, you ensure only classes in the same JAR file can access its contents. This feature provides a level of encapsulation and security for your Java applications.
Spring Boot, a popular Java framework, simplifies the process of creating executable JAR files for Spring-based applications. It automatically generates a manifest file with the necessary entries and includes all the required dependencies in the JAR file. This makes it incredibly easy to package and deploy Spring Boot applications.
To download a JAR file in Java, follow these steps:
JAR files are a fundamental part of Java development, offering a convenient way to package and distribute Java applications and libraries. They provide a self-contained archive that simplifies the deployment process. It makes understanding JAR files in Java crucial, and this guide has covered all necessary aspects you have to know before you proceed.
1. What steps can I follow to execute a JAR file in the Java environment?
Ans: To execute a JAR file in the diverse Java environment, you'll need to use the command "java -jar <filename.jar>". This simple, yet powerful command will initiate the execution process for the specified JAR file.
2. What's the process for extracting files from a JAR file in Java?
Ans: When it comes to extracting files from a JAR file in Java, you can rely on the command "jar xf <filename.jar>". Utilizing this command will efficiently unpack the contents of the designated JAR file.
3. How do I add a file to an existing JAR file?
Ans: You can add a file to an existing JAR file using the 'jar uf' command, followed by the filename and the file to add. The syntax is "jar uf <filename.jar> <file>". This command updates the JAR file with the added file.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.