How to Add JAR file to Classpath in Java?


While developing any Java application, we may require to use external libraries or modules that are packaged as JAR files. To use a JAR file in those Java applications, we need to add it to the classpath, which is a list of locations where the Java runtime can find and load classes. This article aims to explain how to add a JAR file to the classpath. We will start this explanation by introducing JAR Files.

Java JAR File

The full form of JAR is Java Archive File. Java provides this feature to bundle multiple Java program files as well as their corresponding class files into one single unit. Perhaps, it is the only file format that can store audio, video, text files and so forth in one place.

The need for data compression was felt to store and transmit large amounts of data without any hassle. For this purpose, a ZIP file was introduced that encouraged Java creators to develop JAR files. Their primary motive was to archive the Java applet and its components like classes, images and other resources so that it can be downloaded securely through the browser in only one HTTP request

The two main benefits of JAR files are

  • We can fetch multiple Java programs at once.

  • It also compresses the size of file which saves a lot of time required in downloading.

Since it is created using Java programming language, therefore it is also platform independent means a JAR file created on a device can be supported on other devices also.

Till this point, we have understood Java JAR files. Now, let’s discuss how we can add it to the classpath

Using -classpath option

While running the Java application from command line, we can use the –classpath option to add JAR files. For example, if we have a JAR file called ‘mylib.jar’ in the current directory, then we can use the following command:

For Windows −

java -classpath.;mylib.jar MyApp

Here, the dot (.) indicates the current directory and the semi-colon (;) is a separator

For Linux −

java -classpath.:mylib.jar MyApp

In linux, : is used as a separator.

We can also use absolute or relative paths to specify the location of the JAR file.

java CLASSPATH=.;C:/java programs/mylib.jar

Using the CLASSPATH environment variable

Another way of adding a JAR file to the classpath is to use the CLASSPATH environment variable. This variable is a global setting that affects all Java applications that run on our system. The one drawback of this method is that it might cause conflicts among other Java applications.

To set the CLASSPATH variable, we can use the following command:

For Windows

set CLASSPATH=.;mylib.jar

We use the ‘set’ keyword in Windows

For Linux

export CLASSPATH=.:mylib.jar

We use the ‘export’ keyword in Linux.

Using a manifest file

We all know that the execution of a Java program starts from main() method and it is contained in a class. The manifest file contains the name of that class therefore, to run the jar file directly from command line we need a manifest file. It stores the class as a key value pair

Main-Class: MyApp
Class-Path: mylib.jar

Using wildcard option

We can also include multiple JAR files by using the wildcard option. The files that we want to add must be stored in a single directory

The command for this operation is as follows −

java.exe -classpath C:\java programs\* MyApp

Conclusion

Java Archive Files is actually a zip file that enhances the portability of Java files. Through this article, we have learned how to add a JAR file to the classpath in Java using several ways that include: using the –classpath option, manifest file, wildcard option and by setting the CLASSPATH environment variable.

Updated on: 21-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements