Home » Java Basics » java.library.path – What is Java library and how to use

About Bhagvan Kommadi

Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.

java.library.path – What is Java library and how to use

In this tutorial, we will discuss how to set java.library.path. We will explain its definition, and how can be used by Java applications.

The Java Virtual Machine (JVM) uses the java.library.path property in order to locate native libraries. This property is part of the system environment used by Java, in order to locate and load native libraries used by an application.

java.library.path

When a Java application loads a native library using the System.loadLibrary() method, the java.library.path is scanned for the specified library. If the JVM is not able to detect the requested library, it throws an UnsatisfiedLinkError. Finally, the usage of native libraries makes a Java program more platform dependent, as it requires the existence of specific native libraries.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

1. What is a java library and why we use it?

A java library consists of software components that are developed by programmers and reusable. They help in providing different services. A java library is in a deployment format called JAR file. The format is based on pkzip file format. A jar file has java classes and resources such as properties, icons, and other files. A java library file can be used in other Java projects by specifying it in the classpath. The classes in the jar file are accessible to java application after the library is specified in the classpath.

2. How to find a library jar and download it?

A java library can be searched in different repositories such as maven, guava, apache-commons, and others. You can download the java library by specifying the version from these repositories. The java library is specified in the classpath and classes from the library are used in java projects. For example, the database driver libraries can be downloaded from the database vendor repositories. Postgres SQL will be available on the PostgreSQL website.

3. How to set the java.library.path property

There are several ways to set the java.library.path property:

  • Through the command line or terminal: Using the terminal (Linux or Mac) or the command prompt (Windows), we can execute the following command, in order to execute our Java application:
    1
    2
    java -Djava.library.path=<path_to_dll> <main_class>

    where the path_to_dll argument must be replaced with the path of the required library.

  • Through Java source code: Inside an application’s code we can set the java.library.path using the following code snippet:
    1
    2
    System.setProperty(“java.library.path”, “/path/to/library”);
  • Through an IDE: The java.library.path can be configured using an IDE, such as Eclipse or Netbeans.

4. Setting the java.library path. using Eclipse

In order to define the java.library.path property in Eclipse, the following steps must be completed:

  • Select your project in the Package Explorer area and press a right-click on it.
    1. Select Build PathConfigure Build Path... option.
    2. In the appearing window, select the Libraries tab.
    3. Then, expand the JRE System library option and select the Native library location.
    4. Click on the Edit... button at the right panel.
    5. Locate the required library and then click OK.
    6. Close the window.

If the aforementioned steps have been successfully completed, then the selected project will be executed using the required native library.

5. Setting the java.library path. using Netbeans

In order to define the java.library.path property in Netbeans, the following steps must be completed:

  • Select your project in the Projects area and press a right-click on it.
  • Select Properties and then, move to the Run tab.
  • In the VM Options field, add the following option, based on your library’s path:
    java -Djava.library.path=<path_to_dll>
  • Click on OK in order for the window to close.

If the aforementioned steps have been successfully completed, then the selected project will be executed using the required native library.

6. Top 10 Java standard libraries

Top 10 Java standard reusable libraries are mentioned below:

  • Core Java Libraries
    • java.lang
    • java.util
    • java.io
    • java.nio
    • java.math
    • java.net
  • Java UI Libraries
    • javax.swing
  • java.media
  • Apache Commons
    • commons.math
    • commons.cli
    • commons.csv
    • commons.io
  • spring boot
  • google-gson
  • hibernate-orm
  • Unit Testing Libraries
    • mockito
    • junit
  • log4j
  • Slf4j

7. Create an example in which you use a library

Let us look at creating a Math Library with public api with methods for product and difference of two integers. MathAPI class is shown as below:MathAPI

package org.javacodegeeks.math;
public class MathAPI {

    public static int getProduct(int a, int b){
		return a*b;
	}
	public static int getDifference(int a, int b){
		return a-b;
	}
}

The command used for compilation of the code in the math folder is shown below:Compilation command

javac MathAPI.java

Java library MathAPI.jar is created by using the following command:Library command

jar -cvf MathAPI.jar org

The MathAPI library can be used in MathAPIExample as shown below:MathAPIExample

import org.javacodegeeks.math.MathAPI;

public class MathAPIExample
{


 public static void main(String[] args)
 {
 
   int product = MathAPI.getProduct(3,2);
   
   int difference = MathAPI.getDifference(3,2);
   
   System.out.println("product is "+ product + " difference is "+ difference);
 }
}

The command used for compilation of the code is shown below:Compilation command

javac -classpath MathAPI.jar MathAPIExample.java

The command used for execution of the code is shown below:Execution command

java -cp MathAPI.jar MathAPIExample

The output of the above command when executed is shown below:Execution Output

apples-MacBook-Air:javalibrarypath bhagvan.kommadi$ java -cp MathAPI.jar:. MathAPIExample
product is 6 difference is 1

8. Download the Source Code

That was an article about java.library.path: What is Java library and how to use.

Download
You can download the full source code of this example here: java.library.path – What is Java library and how to use

Last updated on Oct. 06th, 2020

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

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

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Beski
Beski
2 years ago

Hi, I dont use use any IDE when I try to load a native library using System.loadLibray() method in a servlet, I get “UnsatisfiedLinkError”. Can you please give me a solution?

Navod Anushan Katugampala
Navod Anushan Katugampala
1 year ago

System.setProperty(“java.library.path”, “path/opencv/build/java/x64”); Its not work. Please give the solution.

Shavinu Ama
Shavinu Ama
1 year ago

try
System.setProperty(“java.library.path”, “path//opencv//build//java//x64”);