Java library path как добавить

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

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 envir onment 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 the 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 is already written in native languages, and you are just writing some Java code on top of that. In that scenario, instead of rewriting the whole stuff, you tend to use native libraries.

Sometimes a third-party JAR (written in Java) d epends upon some native components as well. If you have been using Tibco RV messaging, then you might know that Java API f or 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.

Читайте также:  Java spring bean interface

Since most of us, run Java programs from Eclipse IDE, it’s important to know How to set java.library.path in Eclipse. In this Java tutorial, I will show you a 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

If you are already familiar wit h 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 a 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 the above steps, you can also set native library location, which will then converted into java.library.path by Eclipse, into the source tab. Each source folder allows you to specify a native library location.

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 the Arguments tab, under VM arguments.

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

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.

  • 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 the JAR file in Eclipse? (guide)
  • Eclipse shortcut to print System.out.println statements? (shortcut)
  • How to increase the 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)

6 comments :

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?

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?

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.

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

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?

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

Источник

Java.library.path setting programmatically

When messing around with JNI, one has to set the java.library.path accordingly. Unfortunately the only way is to add a system property before the application is started:

java -Djava.library.path=/path/to/libs 

Changing the system property later doesn’t have any effect, since the property is evaluated very early and cached. But the guys over at jdic discovered a way how to work around it. It is a little bit dirty – but hey, those hacks are the reason we all love Java…

System.setProperty( "java.library.path", "/path/to/libs" ); Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" ); fieldSysPath.setAccessible( true ); fieldSysPath.set( null, null ); 

Explanation

At first the system property is updated with the new value. This might be a relative path – or maybe you want to create that path dynamically.

The Classloader has a static field ( sys_paths ) that contains the paths. If that field is set to null, it is initialized automatically. Therefore forcing that field to null will result into the reevaluation of the library path as soon as loadLibrary() is called…

+1 — Very cool hack — I will try this as the last resort [this is very cool, but horrible for maintainability!].

Please quote the relevant parts of the article in your answer as links can go dead (which yours has).

You should be really careful with this, not only is it implementation based, but it might also cause unexpected exceptions when a concurrent thread tries to load a native library and has just passed the null check for this field when you set it to null

@GuyTal did you mean your native library(.so or .dll) wrap in jar? then you will need to extract it somewhere outside, on file system first, see stackoverflow.com/questions/20389255/…

No you can’t. This property is a read only value. You can change it at JVM launchin time with:

-Djava.library.path=your_path 

If you want to load a library from a specific location, you can use System.load(libraryPath) instead with the full path to the library.

I wanted to accept both the answers, but as I can accept only one I will go with this,as the cool hack relies on implementation details.

I’m just quoting from the link provided by secmask (https://cedarsoft.com/blog.html), in case the link goes dead:

Changing the system property java.library.path later doesn’t have any effect, since the property is evaluated very early and cached. But the guys over at jdic discovered a way how to work around it. It is a little bit dirty – but hey, those hacks are the reason we all love Java.

System.setProperty("java.library.path", "/path/to/libs"); Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); fieldSysPath.setAccessible(true); fieldSysPath.set(null, null); 

Explanation:

At first the system property is updated with the new value. This might be a relative path – or maybe you want to create that path dynamically. The Classloader has a static field ( sys_paths ) that contains the paths. If that field is set to null , it is initialized automatically.Therefore forcing that field to null will result into the reevaluation of the library path as soon as loadLibrary() is called.

Источник

how to set java library path for processing

and point it to the directory containing the relevant library.

This isn’t working for me. I tried java -Djava.library.path=C:\Python33 but it gave me the usage of java.exe

@papaiatis You still have to specify the name of the class to run. The -D is in addition to what you normally specify when running a java from the command-line.

Before System.loadLibrary(«») , use the following code to check you java.library.path

Generally, the java.library.path = /usr/java/packages/lib/i386:/usr/lib/jni:/lib:/usr/lib

Provides several options for:

Instead of changing your source code, you use the Expressions tab in the debug perspective and put System.getProperty(«java.library.path») to show you what it’s value is.

In Eclipse, I did this to get OpenCV working:

  1. In the Run menu, select Run Configuration.
  2. Go to the (x)=Arguments tab of your sketch.
  3. Add this in the VM arguments field:
-Djava.library.path="/path/to/OpenCV/library" 

Your library.path is fine, what you need to do is to drop prefix lib and suffix .so from your System.loadLibrary( «. » ) . On Linux or «linux-android» those will be automatically added by JVM.

Conclusion of above all answers (short form) is as follows:

Lets say my lib folder path is lib/

Then to add in the library path: run below command:

java -Djava.library.path=lib/ -jar mySampleJar.jar 

Источник

How to set the java.library.path from Eclipse

How can I set the java.library.path for a whole Eclipse Project? I’m using a Java library that relies on OS specific files and need to find a .dll/ .so/ .jnilib . But the Application always exits with an error message that those files are not found on the library path. I would like to configure this whole project to use the library path. I tried to add the path as a VM argument to some run configurations in eclipse but that didn’t work.

16 Answers 16

Don’t mess with the library path! Eclipse builds it itself!

Instead, go into the library settings for your projects and, for each jar/etc that requires a native library, expand it in the Libraries tab. In the tree view there, each library has items for source/javadoc and native library locations.

Specifically: select Project , right click -> Properties / Java Build Path / Libraries tab, select a .jar, expand it, select Native library location, click Edit, folder chooser dialog will appear)

Messing with the library path on the command line should be your last ditch effort, because you might break something that is already properly set by eclipse.

Native library location

that’s a good question. Why do you have more than one folder of native libraries for one jar? I’m pretty sure there is a way where you can embed the native libraries themselves into the jar, so that might be the way to go?

I’m working on a legacy project over which I have very little control. I certainly can’t change the project structure. My workaround for the moment is to put everything on my PATH — but that’s not so nice.

depending on OS, you could create one directory that is all symbolic links to the other libraries? or at least for development purposes you could copy all the libs to one directory.

I can’t make this stick. I can add the native library path, but when I click OK to go all the way back out then jump into project properties again, it says «None». I’m using eclipse Indigo SR2

Источник

Оцените статью