- java.library.path – What is Java library and how to use
- 1. What is a java library and why we use it?
- 2. How to find a library jar and download it?
- 3. How to set the java.library.path property
- 4. Setting the java.library path. using Eclipse
- 5. Setting the java.library path. using Netbeans
- 6. Top 10 Java standard libraries
- 7. Create an example in which you use a library
- 8. Download the Source Code
- Is -Djava.library.path=… equivalent to System.setProperty(“java.library.path”, …)
- 3 Answers 3
- Can I find out what variable java.library.path maps to on the current platform?
- 2 Answers 2
- Java error — cannot find library in java.library.path?
- 2 Answers 2
- What is java.library.path?
java.library.path – What is Java library and how to use
Posted by: Bhagvan Kommadi in Java Basics March 4th, 2014 3 Comments Views
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:
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:
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 the Native library location .
- Click on the Edit. button at the right panel.
- Locate the required library and then click OK .
- 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=
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:
The command used for compilation of the code in the math folder is shown below:
Java library MathAPI.jar is created by using the following command:
The MathAPI library can be used in MathAPIExample as shown below:
The command used for compilation of the code is shown below:
The command used for execution of the code is shown below:
The output of the above command when executed is shown below:
8. Download the Source Code
That was an article about java.library.path: What is Java library and how to use.
Last updated on Oct. 06th, 2020
Is -Djava.library.path=… equivalent to System.setProperty(“java.library.path”, …)
I load an external library that is placed in ./lib . Are these two solutions to set the java.library.path equivalent?
Set path in console when executing jar:
Set path in the code before loading library:
If they are equivalent, why in the second solution can Java not find the library while the first one is ok?
If not, is there a way the set the path in the code?
3 Answers 3
Generally speaking, both approaches have the same net effect in that the system property java.library.path is set to the value ./lib .
However, some system properties are only evaluated at specific points in time, such as the startup of the JVM. If java.library.path is among those properties (and your experiment seems to indicate that), then using the second approach will have no noticeable effect except for returning the new value on future invocations of getProperty() .
As a rule of thumb, using the -D command line property works on all system properties, while System.setProperty() only works on properties that are not only checked during startup.
Although it is not well documented, the java.library.path system property is a «read-only» property as far as the System.loadLibrary() method is concerned. This is a reported bug but it was closed by Sun as opposed to getting fixed. The problem is that the JVM’s ClassLoader reads this property once at startup and then caches it, not allowing us to change it programatically afterward. The line System.setProperty(«java.library.path», anyVal); will have no effect except for System.getProperty() method calls.
Luckily, someone posted a workaround on the Sun forums. Unfortunately, that link no longer works but I did find the code on another source. Here is the code you can use to work around not being able to set the java.library.path system property:
WARNING: This may not work on all platforms and/or JVMs.
Can I find out what variable java.library.path maps to on the current platform?
So far I’ve learned the following about the java.library.path property:
- It’s used when loading native libraries, as opposed to java classes
- Its default value depends on the operating system:
- On Windows, it maps to PATH
- On Linux, it maps to LD_LIBRARY_PATH
- On OS X, it maps to DYLD_LIBRARY_PATH
(Please correct me if I’ve misunderstood any of the above)
I want to modify the value of java.library.path seen by a Java application from the framework I’ve set up to invoke the Java application. I want to do this not by setting the java.library.path property directly, but instead by modifying the system path variable that it maps to. I’d like a clean way to do this that doesn’t have ugly OS-specific code or leave out edge cases if possible.
Is there a way to ask the local Java implementation what environment variable java.library.path maps to?
Then, in a shell script, I’d be able to write something along the lines of:
2 Answers 2
This isn’t a totally unreasonable question, but there’s no good answer, so for posterity, I’ll make an attempt at explaining why you got stuck, and why it won’t work.
java.library.path isn’t guaranteed to be set from an environment variable at all. You can specify what you want it to be with -Djava.library.path= . More than likely, this is what you really want to do anyway. That’s why the option exists.
It turns out (on windows at least), that the environment variable you’re looking for isn’t just used unmolested. Try this code.
Java error — cannot find library in java.library.path?
I am getting a error message like this:
The library libraryname.dll could not be loaded by Windows. Make sure that the library is in you Path environment variable. Exception in thread «main» java.lang.UnsatifiedLinkError: no libraryname in java.library.path.
This error is from me trying to run a jar file on Windows XP via cmd. I am wondering, where exactly is java.library.path? I’ve already added C:\Program Files\Java\jdk1.6.0_26 to my PATH but it still gives me the error. How would you go about debugging this?
2 Answers 2
You can simply pass java.library.path as a system property as shown below:
First you need to find out where the libraryname.dll is and add it above in «path_to_dll».
The error is basically saying it cannot find your native libraries. Java tries to locate your library by looking into java.library.path property
It’s an System environment that you need so Java can find your native libraries when you run your application. Several ways to do it:
- Use java -Djava.library.path=[path to your library] when running your program
From the code you could also do.
Set it up from your IDE. An example for Eclipse can be found in this SO question How to set java.library.path from eclipse
EDIT: A good comment below pointed out that #2 will not working 100% because you might not set this prior to calling getProperty(). Missed that point and thanks for pointing that out.
What is java.library.path?
I wonder what is java.library.path? where should i set this and how?
please guide.
Your application tries to load a native library called liblwjgl.so (Linux) or lwjgl.dll (Windows).
Check to see if this file exists and what it’s file permissions are.
This problem most often shows up because the file does not exist, has incorrect file permissions, or can not be located by the JVM.
Under Linux set LD_LIBRARY_PATH.
Under Windows set PATH.
Thanks, I found the lwjgl.dll files, there are about 4 dll files. Where should i put them?
How to find out that JVM is looking for a ‘.dll’ or ‘.so’ module from the exception thrown?
The message says «java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
ary.path». The «lwjgl» is interpreted in a platform-dependent way. On Windows, it means LWJGL.DLL . On Linux and many other UNIX variants, it means «liblwjgl.so». On other platforms, it may mean something else. You’re just supposed to know what it means on your platform.
Since you’re on windows, just make sure the file(s) are in a directory named on your PATH environment variable; you could add an entry if need be.