How to Create a JAR Software Library in Java

by xanthium-enterprises in Circuits > Software

3688 Views, 1 Favorites, 0 Comments

How to Create a JAR Software Library in Java

create-java-jar-library.jpg

In this tutorial, we will learn

  1. How to create a JAR Software library using Java and JDK Command line Tools
  2. How to use the classes inside the created JAR in your Custom program.

We have an online tutorial

Tutorial uses Java Language and Command line tools from JDK.

Tutorial is aimed at beginners who wants to learn how Java works without using any IDE's

Supplies

This is a software tutorial, So you don't have to buy anything.Following items have to be downloaded if you dont have them

  1. Java Runtime Environment (JRE)
  2. Java Development Kit (JDK)
  3. A Text Editor

Video of How to Create Java JAR Software Library Using JDK

Creating a Java JAR Software Library and using the Classes inside it in Command line

This Youtube video explains the entire process of creating and using a JAR library on a windows platform

Creating a JAR File and Viewing Its Contents Using JDK Tools

You can create a JAR file using the jar utility of the Oracle JDK or GraalVM JDK.

The command for creating the JAR file is shown below.

jar cvf your-jar-file-name.jar input-files
  1. input-files can be java class files, images icons or other files you want to include with your program
  2. your-jar-file-name.jar The name you want to give to your jar file.

To view the contents of your JAR file you can use the tf option as shown below.

jar tf your-jar-file-name.jar 

Adding the Files

class-source-showing-package.jpg

We will learn how to create a multi file class JAR package containing hierarchical package name spaces. 

All our source codes belong to the package  com.mypackage

There are three source files which we will compile and convert to classes.

For this tutorial we will be creating three java files.

  1. JMainEntry.java  - contains the main class 
  2. DBaccess.java   - public dummy class DBaccess
  3. Serial.java       - public dummy class Serial



Compiling the Java Files to Classes

Now we put

  1. DBaccess.java   - public dummy class DBaccess
  2. Serial.java       - public dummy class Serial

file in a directory and use the javac compiler to compile to .class files.

Make sure that all the methods() in Serial.java and DBaccess.java are public. 

javac -d . *.java

this would create a directory structure com\mypackage\all your class files

You can run the class file by 

java com.mypackage.JMainEntry

Creating JAR File Using JDK

use the jar utility to pack all the files into a jar file

jar cvf MyJarLib.jar *

This will create a jar file named MyJarLib.jar

Using the Jar in Your Custom Program

compiling-java-code-jar-file.jpg

Now create a .java file called MyCustomCode.java that will use the classes inside the jar file.

MyCustomCode.java

import com.mypackage.Serial;   // Classes inside JAR
import com.mypackage.DBaccess;  // Classes inside JAR
 
public class MyCustomCode
{
   public static void main (String[] Args)
   {
     Serial S = new Serial(); //Class inside JAR
     S.open();
   }
}

 

You can compile the code as shown below

Compiling Code with JAR library

javac -cp .;MyJARLib.jar MyCustomCode.java

 

Running the Code with JAR library

java -cp .;MyJARLib.jar MyCustomCode