Friday, April 26, 2013

What is java.library.path , How to set in Eclipse IDE

java.library.path is a System property, which is used by Java programming language, mostly JVM, to search native libraries, required by a project. Similar to PATH and Classpath environment variable, java.library.path also includes a list of directory. When Java code loads a native library (a library or executable written in a language like C, C++ or native code) using System.loadLibrary("name of library") method, java.library.path is scanned for specified library. If JVM doesn't found requested native library, then it throws java.lang.UnsatisfiedLinkError: no native library in java.library.path. Now someone may argue that, why does Java program should depend on a native library, doesn't this make a Java application platform dependent? Well, he would be right, but there are situations, where most of the code are already written in native languages, and you are just writing some Java code on top of that. In that scenario, instead of rewriting whole stuff, you tend to use native libraries. 

Sometimes a third party JAR (written in Java) depends upon some native components as well. If you have been using Tibco RV messaging, then you might know that Java API for Tibco RV depends upon several dll files, and until those files are present in java.library.path, you can not run that Java program successfully. 

Since most of us, run Java program from Eclipse IDE, it's important to know How to set java.library.path in Eclipse. In this Java tutorial, I will show you couple of ways to set java.library.path in Eclipse IDE, you can follow similar steps in other IDE like Netbeans or IntelliJ to set java.library.path.

3 Ways to set java.library.path in Eclipse IDE

What is java.library.path , How to set PATH in EclipseIf you are already familiar with setting PATH and Classpath in Java, then this shouldn't be a problem. Since java.library.path is a system property, most common way to set this is by providing as JVM arguments. Since at low level (if you are using start-up scripts), Java program starts with "java" command, you can provide them system property using -Dpropery=value. In order to set java.library.path you can provide -Djava.library.path=C:\Windows to set it in Windows. By the way, it's little different in Eclipse, which we will see in next section, which shows three places to set java.library.path in Eclipse.

1) You can set java.library.path in Eclipse by providing native library location for you libraries inside "java build path". Just follow below steps :

        1.1) Select Project ==> Properties ==> Java Build Path ==> Libraries ==> JRE System Library ==> Native library location ==> Edit

        1.2) Edit will open a dialog box, which allows you to choose an external folder, or a workspace location, to find native libraries

2) Similar to above steps, you can also set native library location, which will then converted into java.library.path by Eclipse, into source tab. Each source folder allows you to specify a native library location.

       1.1) Open Java Build Path as shown in above step, then choose Source tab
       1.2) Each Source folder has one native library location, select and edit them to include your native libraries

3) A third way to set java.library.path is by using VM arguments. Open Run Configurations or Debug Configuration of your project and provide -Djava.library.path="native library path" in Arguments tab, under VM arguments.

So, you can see It's not difficult to set java.library.path in Eclipse. It's also worth noting that, this system property is only read, when JVM startup. IF you further change this System property using System.setProperty("java.library.path", "new path"), it won't take into effect.

Further Learning
The Eclipse Guided Tour - Part 1 and 2
Java Fundamentals, Part 1 and 2
The Java Developer's Guide to Eclipse

Other Java Eclipse articles you may like to explore
  • 30 Useful Eclipse Shortcuts for Java Developers (list)
  • How to remote debug Java application in Eclipse? (tutorial)
  • 10 Eclipse debugging tips Java developer should know? (see here)
  • How to attach source code for JAR file in Eclipse? (guide)
  • Eclipse shortcut to print System.out.println statements? (shortcut)
  • How to increase console buffer size in Eclipse? (steps)
  • How to use spaces instead of tabs in Eclipse? (guide)
  • How to create an executable JAR file from Eclipse? (example)
  • 3 Books to Learn Eclipse IDE for Java developers (list)
  • How to Increase Heap Size of Java Program running in Eclipse? (guide)

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a comment.


Anonymous said...

Doesn't by using native library or code, you risk your Java program of making platform dependent? Isn't it against of Java's motive of creating application which is platform independent?

Pooja said...

I have never set that java.library.path and never faced any issue. Do you know that is the default value of java.library.path, and why it's not a problem to not set this?

Anonymous said...

Few things to note about java.library.path system property :

1) java.library.path is used to search native libraries used by your Java application e.g. tibco rv binaries, .dll files in windows and .so files in Linux.

2) If you don't provide explicit value for java.library.path, it is automatically set to PATH by JVM in Windows operating system and to the value of LD_LIBRARY_PATH in UNIX e.g. Solaris or Linux.

3) They only contains location of native binaries not JAR files.

4) If your native libraries are not located on those location then you will get unsatisfied link error.

Anonymous said...

You can also check value of this system property by using following code :
System.out.println("java.library.path : " + System.getProperty("java.library.path"));

Anonymous said...

I have performed the steps as mentioned by specifying the path of the DLL under the native library but still the DLL is not found?

Javin said...

@Anonymous, are you trying in Eclipse or command line?

Post a Comment