- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a JAR file?
A java archive file is a file format/ archiving tool which contains all the components of an executable Java application. All the predefined libraries are available in this format.
To include any of these (other than rt.jar) in to your project you need to set the class path for this particular JAR file. You can create a JAR file using the command line options or using any IDE’s.
Creating a Jar file
You can create a Jar file using the jar command as shown below.
jar cf jar-file input-file(s)
Let us consider an example, create a Sample Java program with name Sample.java
Sample.java
public class Sample { public static void main(String args[]){ System.out.println("Hi welcome to Tutorialspoint"); } }
If you compile this program using Javac command as shown below −
C:\Examples >javac Sample.java
This command compiles the given java file and generates a .class file (byte code)
Now, create a jar file for the generated class as −
C:\Sample>jar cvf sample.jar *.class added manifest adding: Sample.class(in = 434) (out= 302)(deflated 30%)
This will generate a jar file for all the classes in the current directory (since we used * instead of name) with specified name.