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

About Sotirios-Efstathios Maneas

Sotirios-Efstathios Maneas
Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

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

In this tutorial we will discuss about java.library.path, 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.

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 as Eclipse or Netbeans.
  • Setting the java.library path. using Eclipse

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

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

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

    Setting the java.library path. using Netbeans

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

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

    4. 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.

     
    This was a tutorial about the java.library.path property in Java.

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 ....

 

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close