Home / Java Techs / How to create an Executable Jar File

How to create an Executable Jar File

Print Friendly, PDF & Email

In Java programming, it is common to combine several classes in one .jar (“java archive”) file.  Library classes are released in this fashion which these classes are packed into a single jar file so that it can be used flexible and portable.  Larger projects use these jar files by importing them as dependencies.  You can create your own jar file combining several classes, too.

jar files are created using the jar  utility program from the JDK.  You can make your jar file runnable by telling jar  which class has main .  To do that, you need to create a manifest file.  A manifest is a one-line text file with a “Main-Class” directive.  For example:

Main-Class: com.itersdesktop.javatechs.Demo

Notes: This line must end with a newline.

A jar file created with a main class manifest can be used both as a library and a runnable jar.  If you use it as a library, you can edit and compile any of the classes included in the jar, and add it to your project.  Then it will override the one in the jar file. You can run a jar file dependently.

You can create a manifest file in any text editor, or even by using the echo  or cat   command.  You can give your manifest file any name, but it’s better to use something standard, such as manifest.txt .

Once you have a manifest and all your classes have been compiled, you need to run JDK’s jar  utility.  This file jar  takes command-line arguments; if you run it without any arguments, it will display the usage information and examples.  You need

tnguyen@tnguyen-rh6> 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.

How to create an executable jar file

How to create an executable jar file

Actually, if your manifest contains only the Main-Class directive, you can specify the main class directly on the jar ‘s command line, using the e  switch, instead of m .  Then you do not need a separate manifest file; jar  will add the required manifest to your jar file for you. For example:

tnguyen@tnguyen-rh6> jar cvfe MyJarName.jar MyMainClass *.class

Below is a reference for creating a jar file in Eclipse and the detailed steps for doing this in Command Prompt and in JCreator.

Creating a jar File in Eclipse

In Eclipse Help contents, expand Java development user guide ==> Tasks ==> Creating JAR files.  Follow the instructions for Creating a new JAR file or Creating a new runnable JAR file.

The JAR File and Runnable JAR File commands are for some reason located under the File menu: click on Export… and expand the Java node.

Creating a jar File in JCreator

You can configure a “tool” that will automate the jar creation process.  You only need to do it once.

  1. Click on Configure/Options.
  2. Click on Tools in the left column.
  3. Click New, and choose Create Jar file.
  4. Click on the newly created entry Create Jar File in the left column under Tools.
  5. Edit the middle line labelled Arguments: it should have
    cvfm $[PrjName].jar manifest.txt *.class
  6. Click OK.

Now set up a project for your program, create a manifest file manifest.txt  or copy and edit an existing one.  Place manifest.txt  in the same folder where the .class  files go.  Under View/Toolbars  check the Tools  toolbar.  Click on the corresponding tool button or press Ctrl-1  (or Ctrl-n  if this is the n -th tool) to run the Create Jar File  tool.

With Windows Explorer, go to the jar file that you just created and double click on it to run.

Creating a jar File in Command Line

  1. Start Command Prompt/Terminal/Console.
  2. Navigate to the folder that holds your class files:
    tnguyen@tnguyen-rh6>cd /Dropbox/Study/Java/Demo
  3. Set path to include JDK’s bin.  For example:
    tnguyen@tnguyen-rh6>path c:\Program Files\Java\jdk1.7.0_25\bin;%path%
  4. Compile your class(es):
    tnguyen@tnguyen-rh6>javac *.java
  5. Create a manifest file and your jar file:
    tnguyen@tnguyen-rh6>echo Main-Class: com.itersdesktop.javatechs.Demo > manifest.txttnguyen@tnguyen-rh6>jar cvfm MyApp.jar manifest.txt *.class
    tnguyen@tnguyen-rh6>jar cvfe MyApp.jar com.itersdesktop.javatechs.Demo *.class
  6. Test your jar:
    tnguyen@tnguyen-rh6>java -cp MyApp.jar com.itersdesktop.javatechs.Demo
    tnguyen@tnguyen-rh6>java -jar MyApp.jar

Hopefully you find this post interesting and useful for your study and work.
Welcome all sincere comments and let your sympathy in the comment sections!

About Admin

programming all time and taking a bread for the lunch hahaha... When you are engaging your work, nobody can attract you doing another thing. Otherwise, should should move to other jobs or are not interested in your job enough.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *




This site uses Akismet to reduce spam. Learn how your comment data is processed.