How to Create JAR Files in Java

Sarwan Soomro Feb 02, 2024
  1. Importance of Creating .jar Files in Java
  2. Use Eclipse IDE to Create .jar Files in Java
  3. Use Windows Command Prompt to Create .jar Files in Java
How to Create JAR Files in Java

This tutorial will show you how to create multiple .jar files using your favorite IDE and Windows commands. In addition, it will also give you a live demonstration of using .jar files between various Java projects.

We will cover the exporting and importing of .jar files and the significance of using them for your production. The first section shows creating .jar files using IDE, and the second with Windows Command Prompt.

Importance of Creating .jar Files in Java

For example, you have more than one Java project, a .jar file, in that case, can be used to access classes that represent specific code blocks.

Also, it reduces the repetition of code and enhances reusability. Meanwhile, we can also email our .jar files to team members working on the same project for real-time production.

That said, Java has its way of dealing with its files, just like other programming languages. For example, we create a .shell file for Bash programming and .exe files for .NET.

Similarly, Oracle has Java archive files (.jar).

Note
You can create dependency of your .jar file using Maven.

Read more about it here: Guide to installing 3rd party JARs

Use Eclipse IDE to Create .jar Files in Java

Though we use Eclipse to create our first .jar file, most IDEs can do the same. It is simply a matter of your choice.

First, we will create two projects.

Projects

Check out the Accessible class from the MyJar project.

package Myjar1; // it is a best practice to use classes under specific packages

public class Accessible { // we will export it as a jar and then import it in our other projects

  public int add(int a, int b) { // a demo (you can customize it, create more functions, etc. The
                                 // objective is to use a class as a jar file)
    System.out.println("Access me!");
    return a + b;
  }
}

Now check the AccessMyJarFile class from the access my jar file project.

import Myjar1.Accessible; //we are importing it now
public class AccessMyJarFile {
  public static void main(String[] args) { // do not forget to use main functions

    Accessible a = new Accessible(); // constructor
    System.out.println(a.add(100, 200));
  }
}

Output:

Access me!
300

We showed you the output of the second code block that successfully accesses the first using import Myjar1.Accessible;, let’s understand how to export and import .jar files.

In this way, you can create multiple classes and use them as .jar files.

Note
When dealing with .jar files, all your projects must use their respective packages and main functions.

If you’re still confused, the following two bullet points indicate how to export and import a .jar file from one project to another.

  1. Export a .jar file:

    For eclipse users, click right on the project and then export.

    exporting jar file in Eclipse

    Choose the highlighted sections, click Next, name your .jar file and save it. That is it!

  2. Import a .jar file:

    This might be the simplest part of this whole process. It is the same as importing other reusable .jar files every day.

    Nevertheless, we have highlighted this part, especially for those of you who are beginners.

    importing jar file in eclipse

Use Windows Command Prompt to Create .jar Files in Java

It is no doubt that IDEs make it easy when it is about development.

But, we highly recommend you practice creating your .jar files using commands, especially if you want to learn more.

It is pretty easy to do. Let us break it down into steps not to miss the essentials.

  1. First, create a Manifest.txt file.

    create a manifest file

    This example shows a single class, NewJar. We can also define multiple classes inside it.

  2. Second, you must have a class with a Java file extension basic demo.

    public class Myjar {
      public static void main(String[] args) {
        System.out.println("I create a jar file!");
      }
    }
    
  3. Compile it:

    Command: javac Myjar.java - it will create a class Myjar.class file.

  4. Create a .jar file using Manifest.txt.

    Command: jar cfe Jar.jar Manifest.txt Jar.class

    Output:

    create a jar file using manifest file

Note
With JDK6, you might also want to use the e to declare the entry point, just like Manifest.txt does.

Command: jar cfe myJar.jar Jar Jar.class

  1. c: Temporarily changes the directory.
  2. f: Generally, it indicates the JAR file name as a second argument.
  3. e: It is the entry point.

After cfe, myJar.jar represents the JAR file that will be created, and Jar is the main class name followed by the compiled Jar.class.

Output:

create a jar file without manifest file

Sarwan Soomro avatar Sarwan Soomro avatar

Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.

LinkedIn

Related Article - Java Jar