JAR FILES A java is a / 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)

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.

How to create a JAR file? You can create a JAR file using the following command. jar cf jar-file input-file(s) You can also create JAR files using IDE’s. To create a JAR file using eclipse follow the procedure given below. Open the Jar File wizard The Jar File wizard can be used to export the content of a project into a jar file. To bring up the Jar File wizard −

 In the Package Explorer select the items that you want to export. If you want to export all the classes and resources in the project just select the project.  Click on the File menu and select Export.  In the filter text box of the first page of the export wizard type in JAR.

 Under the Java category select JAR file.  Click on Next. Using the Jar File wizard In the JAR File Specification page – Enter the JAR file name and folder.  The default is to export only the classes. To export the source code also, click on the Export Java source files and resources checkbox.

 Click on Next to change the JAR packaging options.  Click on Next to change the JAR Manifest specification

 Click on Finish.

How to run a JAR file through command prompt in java? For packaging of class files Java provides a file format known as JAR (Java Archive). Typically, a JAR file contains .class files, images, text files, libraries that are required to execute the application or, library. If you have any library in this format to use it In your application either you need to place it in the current (or, lib) folder of the project or, you need to set the class path for that particular JAR file. Creating a Jar file Java provides jar command to work with jar files if you execute it in the command prompt you will get the execution syntax and options of this command as shown below −

C:\>jar

Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...

Options:

-c create new archive

-t list table of contents for archive

-x extract named (or all) files from archive

-u update existing archive

-v generate verbose output on standard output

-f specify archive file name

-m include manifest information from specified manifest file

-n perform Pack200 normalization after creating a new archive

-e specify application entry point for stand-alone application bundled into an executable jar file.

-0 store only; use no compression

-P preserve leading '/' (absolute path) and ".." (parent directory) components from file names

-M do not create a manifest file for the entries -i generate index information for the specified jar files

-C change to the specified directory and include the following file

You can create a JAR file using by executing this command with the options c, v, f Following is the syntax to create a JAR file using command prompt − jar cvf jar_file_name output_path

Creating a JAR file using eclipse You can also create JAR files using IDE’s. To create a JAR file using eclipse follow the procedure given below −

 Open eclipse, create a project in it as −

 Right Click on the project folder and select the Export option as

 Under the Java category select JAR file.

 Click on Next.

 Enter the JAR file name and folder.

 Click on Finish.