Let us see how to create a JAR file using Javas jar command as well as using Eclipse IDE. The JAR file format is based on the popular ZIP file format. Usually these file are used for archiving and distribution the files and implementing various libraries, components and plug-ins in java applications. Compiler and JVMs (Java Virtual Machine) can understand and implement these formats for java application.

JAR files are the output of the Java Archive Tool. The Java Archive Tool is a program that is part of the Java Standard Edition Software Development Kit.). Before using a jar command we need to set the classpath. . Generally the path is where the OS looks to run programs. jar files are created using the jar.exe utility program from JDK. We can make our jar file runnable .to do that, you first need to create a manifest file.  A manifest is a one-line text file with a "Main-Class" directive.  For example:

Main-Class: DanceStudio

 

A jar file created with a main class manifest can be used both as a library and a runnable jar. See the following command to create executable jar file

jar cvfm MyJarName.jar manifest.txt *.class

cvfm means "create a jar; show verbose output; specify the output jar file name; specify the manifest file name."  This is followed by the name you wish to give to your jar file, the name of your manifest file, and the list of .class files that you want included in the jar.  *.class means all class files in the current directory.

 

Following are the steps to create a jar file from the command prompt

  Start Command Prompt.

  Navigate to the folder that holds your class files:

  C:\>cd \mywork

  Set path to include JDKs bin.  For example:

  C:\mywork> path c:\Program Files\Java\jdk1.5.0_09\bin;%path%

  Compile your class(es):

  C:\mywork> javac *.java

  Create a manifest file: 

  C:\mywork> echo Main-Class: DanceStudio >manifest.txt

  Create a jar file:

  C:\mywork> jar cvfm DanceStudio.jar manifest.txt *.class

  Test your jar:

  C:\mywork> DanceStudio.jar

To create a  JAR file in the Eclipse workbench:

  1. In the Package Explorer, we can optionally pre-select one or more Java elements to export. These will be open the wizard page.
  2. Either from the context menu or from the menu bar's File menu, select Export.
  3. Expand the Java node in the wizard page and select JAR file. Click Next.
  4. In the JAR File Specification page, select the resources that we want to export in the Select the resources to export field.
  5. Select the appropriate checkbox to specify whether we want to Export generated class files and resources or Export Java source files and resources. Note: Selected resources are exported in both cases.
  6. In the Select the export destination field, either type or click Browse to select a location for the JAR file.
  7. Select or clear the Compress the contents of the JAR file checkbox.
  8. Select or clear the Overwrite existing files without warning checkbox. If we clear this checkbox, then we will be prompted to confirm the replacement of each file that will be overwritten.
  9. Note: The overwrite option is applied when writing the JAR file, the JAR description, and the manifest file.
  10. Now we have two options:
  11. Click Finish to create the JAR file immediately.
  12. Click Next to use the JAR Packaging Options page to set advanced options, create a JAR description, or change the default manifest.
  13. If we want to save the JAR file description, select the Save the description of this JAR in the workspace checkbox. A JAR file description can be used to regenerate a JAR file without using the wizard.
  14. The compiler is able to generate CLASS files even when source contains errors. We have the option to exclude CLASS (but not source) files with compile errors. These files will be reported at the end, if reporting is enabled.
  15. We can choose to exclude CLASS (but not source) files that have compile warnings. These files will be reported at the end.
    Note: This option does not automatically exclude class files with compile errors.
  16. We can choose to include the source folder path by selecting the Create source folder structure checkbox.
  17.  Select the Build projects if not built automatically checkbox if you want the export to perform a build before creating the JAR file.
  18. Click Finish to create the JAR file immediately or Next if we want to change the default manifest.
  19. If it is not already selected, click the Generate the manifest file button.
  20. We can now choose to save the manifest in the workbench. This will save the manifest for later use. Click Save the manifest in the workspace, then click Browse next to the Manifest file field to specify a path and file name for the manifest.
  21. If we decided to save the manifest file in the previous step and you chose to save the JAR description on the previous wizard page, then you can choose to reuse it in the JAR description (by selecting the Reuse and save the manifest in the workspace checkbox). This means that the saved file will be used when the JAR file is recreated from the JAR description.This option is useful if you want to modify or replace the manifest file before recreating the JAR file from the description.
  22. We can choose to seal the JAR and optionally exclude some packages from being sealed or specify a list with sealed packages. By default, nothing is sealed.
  23. Click the Browse button next to the Main class field to specify the entry point for our applications.
    Note: If our class is not in the list, then we forgot to select it at the beginning.   Click Finish. This will create the JAR, and optionally a JAR description and a manifest file.



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)