How to add native library to “java.library.path” with Eclipse launch (instead of overriding it)

StackOverflow https://stackoverflow.com/questions/661320

Question

I got a native library that needs to be added to java.library.path. With JVM argument -Djava.library.path=path... I can set the path as I want.

My problem is that my other library (pentaho reporting) searches fonts based on the default java.library.path (including system directories etc) and the manual setting overrides the default path..

So : how can I add a path entry to the default java.library.path instead of overriding it (which seems to be done with -Djava.library.path)? (I wouldn't want to add the default path by hand, which wouldn't be nice for the sake of deployment)

EDIT: Sorry for missing details; I'm working with Eclipse. (The deployment is done with JNLP and there I can use nativelib under resources)

Was it helpful?

Solution

Had forgotten this issue... I was actually asking with Eclipse, sorry for not stating that originally. And the answer seems to be too simple (at least with 3.5; probably with older versions also):

Java run configuration's Arguments : VM arguments:

-Djava.library.path="${workspace_loc:project}\lib;${env_var:PATH}"

Must not forget the quotation marks, otherwise there are problems with spaces in PATH.

OTHER TIPS

If you want to add a native library without interfering with java.library.path at development time in Eclipse (to avoid including absolute paths and having to add parameters to your launch configuration), you can supply the path to the native libraries location for each Jar in the Java Build Path dialog under Native library location. Note that the native library file name has to correspond to the Jar file name. See also this detailed description.

SWT puts the necessary native DLLs into a JAR. Search for "org.eclipse.swt.win32.win32.x86_3.4.1.v3449c.jar" for an example.

The DLLs must be in the root of the JAR, the JAR must be signed and the DLL must appear with checksum in the META-INF/MANIFEST.MF for the VM to pick them up.

In Windows, like this:

-Djava.library.path="C:/MyLibPath;%PATH%"

%PATH% is your old -Djava.library.path

https://bugs.eclipse.org/bugs/show_bug.cgi?id=102239 states that there is no substitution mechanics implemented in Eclipse's launcher, at least no up to release Juno.

Thus it is (almost) impossible to append or prepend another library folder to java.library.path when launching Eclipse without prior knowledge of the default setting.

I wrote almost, cause it should be possible to let Eclipse startup, dump the content of java.library.path and stop Eclipse in one command. The dump would the be parsed and then taken as the input for launching Eclipse, i.e.

#!/bin/bash
# get default value of java.library.path (somehow)
default_lib_path=$( start_dump_stop_eclipse_somehow )  

# now launch Eclipse
eclipse --launcher.appendVmargs \
         -vmargs \
         -Djava.library.path="/my/native/lib/folder:${default_lib_path}"

Can you get round this by calling System.load() programmatically to load your native library? This method (unlike System.loadLibrary()) allows you to specify an absolute path.

In UNIX systems, you can append to the LD_LIBRARY_PATH environment variable. On Windows, the JVM automatically sets the system property, java.library.path, to PATH; so if the dll is on your PATH, then you're set.

Window->Preferences->Java->Installed JREs. Then choose your current JRE(JDK) and click Edit. Fill Default VM Arguments: -Djava.library.path=/usr/local/xuggler/lib. Done!

The solution offered by Rob Elsner in one of the comments above works perfectly (OSX 10.9, Eclipse Kepler). One has to append their additional paths to that separated by ":".

You could also use ${system_property:java.library.path} – Rob Elsner Nov 22 '10 at 23:01

The native library file name has to correspond to the Jar file name. This is very very important. Please make sure that jar name and dll name are same. Also,please see the post from Fabian Steeg My download for jawin was containing different names for dll and jar. It was jawin.jar and jawind.dll, note extra 'd' in dll file name. I simply renamed it to jawin.dll and set it as a native library in eclipse as mentioned in post "http://www.eclipsezone.com/eclipse/forums/t49342.html"

For some reason I couldn't get multiple folders to work (well it did for a while but as soon as I needed more dlls and added more folders, none with white spaces in the path). I then copied all needed dlls to one folder and had that as my java.library.path and it worked. I don't have an explanation - if anyone does, it would be great.

Many of the existing answers assume you want to set this for a particular project, but I needed to set it for Eclipse itself in order to support integrated authentication for the SQL Server JDBC driver.

To do this, I followed these instructions for launching Eclipse from the Java commandline instead of its normal launcher. Then I just modified that script to add my -Djava.library.path argument to the Java commandline.

On Windows, I have found that the important thing is to start Eclipse from the command line rather than from the Start Menu or a shortcut, provided that the native DLL is in a directory in your PATH. Apparently, this ensures that the proper directory is on the path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top