99

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)

JoshDM
  • 4,939
  • 7
  • 43
  • 72
Touko
  • 11,359
  • 16
  • 75
  • 105
  • 1
    pls see accepted answer for this question instead - for me it's much better: http://stackoverflow.com/questions/957700/how-to-set-the-java-library-path-from-eclipse – laher Jun 19 '11 at 23:03
  • The question "how to add native library to .." is confusing. It is probably about adding (appending or prepending) another "library path" , i.e. assuming that Eclipse shows something like java.library.path=path1:path2:path3 by default, the question is how to start Eclipse ending up in java.library.path=my/lib/folder:path1:path2:path3 – wh81752 Feb 21 '14 at 13:20

14 Answers14

52

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.

Touko
  • 11,359
  • 16
  • 75
  • 105
  • 7
    If there are two shared libraries, one dependent on the other, this will not work. The first is found by the Java runtime, but the second is resolved by the system dynamic loader. The only solution I have found is to set LD_LIBRARY_PATH. – kevin cline Nov 27 '12 at 22:52
  • 5
    The answer given by @Touko does not fit to the original question which is about appending or prepending a native library folder. At least on Mac OS 10.8, neither $PATH nor $LD_LIBRARY_PATH nor ${workspace_loc:project}\lib has something to do with the default value. For example, on my Mac the default value is $HOME/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:. – wh81752 Feb 21 '14 at 13:30
  • If you are using TestNG in Eclipse, you will need to edit the TestNG run configuration: - In the Run Configurations window, select your target TestNG configuration. - Select the Environment tab - Add a PATH variable and set its value to your target - Leave the defaulted "Append environment to native environment" toggle. – Philippe Feb 21 '14 at 16:29
  • Can I get an example. – basickarl Nov 24 '14 at 17:55
  • @kevincline would you mind to elaborate why exactly it won't work in that case? I have just run into that problem and I am trying to understand what is wrong with this solution. – anula Feb 10 '15 at 13:11
14

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.

Fabian Steeg
  • 44,988
  • 7
  • 85
  • 112
  • 5
    -1. You're assuming the end-user is running the application from an IDE, which is unlikely. – finnw Jan 24 '10 at 19:38
  • @finnw I see your point. I found the question looking for a solution on how to add a native library in the IDE during development, without overriding `java.library.path` and came back after finding the solution elsewhere. Will edit my answer to make that clearer. – Fabian Steeg Jan 24 '10 at 23:43
  • Actually I'm working with Eclipse even though I didn't mention it at the question. – Touko Feb 22 '10 at 08:38
  • This actually doesn't work in the current version of Eclipse(Luna) because setting the property overrides java.library.path like the user describes as being a problem in the question. – Alex N. May 12 '15 at 15:31
9

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 2
    How can I do it with NetBeans? – Maciek Sawicki Dec 04 '09 at 06:48
  • AFAIK, NetBeans uses Ant to build the project. Read the documentation for Ant how to create signed JARs and how to put things like DLLs into the manifest. – Aaron Digulla Dec 04 '09 at 08:20
  • How do I set it to add the .dll, in Eclipse? – NoBugs Feb 06 '12 at 22:35
  • @NoBugs: In Eclipse, this post should help: http://www.eclipsezone.com/eclipse/forums/t49342.html – Aaron Digulla Feb 13 '12 at 11:12
  • @AaronDigulla do you suggest that DLL may be always attached in this way ? I thought that it only searches something like `java.library.path`. Do you suggest that it only seach root of `jar` ? –  Mar 09 '17 at 20:11
  • @user6023611 No, both ways work. It depends on what you want to achieve. If you have an existing system library which you want to make available to Java code, `java.library.path` is the way to go. For custom DLLs which you wrote, putting them into a JAR is easier to consume. – Aaron Digulla May 30 '17 at 13:42
5

In Windows, like this:

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

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

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
andy boot
  • 11,355
  • 3
  • 53
  • 66
2

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.

geowa4
  • 40,390
  • 17
  • 88
  • 107
  • The thread starter was very specific about how to "append" a second native library folder, don't you agree? – wh81752 Mar 29 '13 at 16:21
  • @user667073 I have been saying the same... APPENDING is the question, the thread-starter already knows how to load a shared lib otherwise ;-) – Ustaman Sangat Apr 25 '13 at 00:27
  • STS 3.4.0 and Mac OS 10.8: The settings of LD_LIBRARY_PATH do not have any impact on java.library.path. I tested this with Subclipse/JavaHL. This one worked: STS --launcher.appendVmargs -vmargs -Djava.library.path=/opt/local/lib while export LD_LIBRARY_PATH=/opt/local/lib ; STS had was negative. – wh81752 Feb 21 '14 at 14:39
2

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}"
wh81752
  • 869
  • 9
  • 16
2
  1. On Windows: Add the path to the library to the PATH environment variable.
  2. On Linux: Add the path to the library to the LD_LIBRARY_PATH environment variable.
  3. On Mac: Add the path to the library to the DYLD_LIBRARY_PATH environment variable.

java.library.path is initilized with the values of the variables above on its corresponding platform.

This should work on any IDE.

You can test if the value is what you expect by calling java -XshowSettings:properties

rboc
  • 344
  • 3
  • 10
2

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.

Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
1

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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Johan
  • 266
  • 3
  • 5
1
  1. Window->Preferences->Java->Installed JREs.
  2. Choose your current JRE(JDK) and click Edit.
  3. Fill Default VM Arguments: -Djava.library.path=/usr/local/xuggler/lib.
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Vadym
  • 35
  • 1
  • 2
    This answer is useless because the thread starter specifically asks about "How to add a native library [..] instead of overriding [..]". This answer just overrides the default setting. – wh81752 Feb 21 '14 at 13:35
0

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.

CrazyPyro
  • 3,257
  • 3
  • 30
  • 39
0

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"

Dhana
  • 9
  • 1
0

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.

Ustaman Sangat
  • 1,505
  • 1
  • 14
  • 26
  • Well, if a native library is found via java.library.path and you copy another one into that folder, then it appears natural that the second one is also found, isn't it? – wh81752 Mar 29 '13 at 16:19
  • Of course, that was why I used that, duh! What I was wondering was whether one could add multiple folders to it without having to move or symlink stuffs around. – Ustaman Sangat Apr 03 '13 at 15:58
-2

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.

Alan
  • 1,889
  • 2
  • 18
  • 30
  • Correct me if I'm wrong but I believe that the current working directory on Windows is automatically included when searching for DLLs (or executable binaries). Thus when you change into the folder where the DLLs are and start then Eclipse up, then yes, the DLLs you are looking for are found. Apart from that, how is this answer related to the thread starters question? – wh81752 Feb 21 '14 at 13:47