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.
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.
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:
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:System.setProperty(“java.library.path”, “/path/to/library”);
- Through an IDE: The
java.library.path
can be configured using an IDE, such asEclipse
orNetbeans
.
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.- Select
Build Path
→Configure Build Path...
option. - In the appearing window, select the
Libraries
tab. - Then, expand the
JRE System library
option and select theNative library location
. - Click on the
Edit...
button at the right panel. - Locate the required library and then click
OK
. - Close the window.
- Select
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 theRun
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.
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
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?
That most likely means the library itself has dynamic link dependencies that aren’t satisfied by the system library path. On Linux, you can view the dependencies for a shared library (libsomething.so) using the ldd command from a terminal. For example, on my system I have /usr/local/lib64/python3.10/site-packages/opencv_python.libs/ which contains library dependencies for OpenCV modules. So, if I examine the FreeType shared library in that directory with ldd: $ ldd /usr/local/lib64/python3.10/site-packages/opencv_python.libs/libfreetype-c0e61f0c.so.6.14.0 linux-vdso.so.1 (0x00007ffd61767000) libz.so.1 => /lib64/libz.so.1 (0x00007f6623b3b000) libbz2-a273e504.so.1.0.6 => not found libpng15-ce838cd1.so.15.13.0 => not found libc.so.6 => /lib64/libc.so.6 (0x00007f6623622000) /lib64/ld-linux-x86-64.so.2 (0x00007f6623b9d000) There are two “not found” dependencies, because that copy of libfreetype… Read more »
System.setProperty(“java.library.path”, “path/opencv/build/java/x64”); Its not work. Please give the solution.
try
System.setProperty(“java.library.path”, “path//opencv//build//java//x64”);
This entry has some good info in the middle, but it both starts and ends VERY wrong because it confuses java packages (JAR files) with native libraries. In item #1, a JAR file is NOT a library. java.library.path has nothing to do with finding JAR files. That’s what java.class.path (AKA the classpath) is for. java.library.path controls the path to load NATIVE libraries from — .so files on linux, .dylib files on macOS, or .dll files on Windows. In item #7, the code given does not provide an example of using a library. MathAPI is a Java class, not a native… Read more »