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

297,486

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

In Windows, like this:

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

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

Solution 5

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}"
Share:
297,486
Apollon
Author by

Apollon

Updated on May 20, 2020

Comments

  • Apollon
    Apollon almost 4 years

    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)

  • Maciek Sawicki
    Maciek Sawicki over 14 years
    How can I do it with NetBeans?
  • Aaron Digulla
    Aaron Digulla over 14 years
    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.
  • Saurabh
    Saurabh over 14 years
    -1. You're assuming the end-user is running the application from an IDE, which is unlikely.
  • Fabian Steeg
    Fabian Steeg over 14 years
    @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.
  • Apollon
    Apollon about 14 years
    Tried this idea but it resulted as java.library.path : D:\Workspace\myProject\lib;%PATH%
  • Apollon
    Apollon about 14 years
    Actually I'm working with Eclipse even though I didn't mention it at the question.
  • Rob Elsner
    Rob Elsner over 13 years
    You could also use ${system_property:java.library.path}
  • NoBugs
    NoBugs about 12 years
    How do I set it to add the .dll, in Eclipse?
  • Aaron Digulla
    Aaron Digulla about 12 years
    @NoBugs: In Eclipse, this post should help: eclipsezone.com/eclipse/forums/t49342.html
  • kevin cline
    kevin cline over 11 years
    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.
  • wh81752
    wh81752 about 11 years
    The default on UNIX/Mac/GNU Linux is LD_LIBRARY_PATH. PATH is a Windows thing.
  • wh81752
    wh81752 about 11 years
    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
    wh81752 about 11 years
    The thread starter was very specific about how to "append" a second native library folder, don't you agree?
  • Ustaman Sangat
    Ustaman Sangat about 11 years
    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
    Ustaman Sangat about 11 years
    @user667073 I have been saying the same... APPENDING is the question, the thread-starter already knows how to load a shared lib otherwise ;-)
  • wh81752
    wh81752 about 10 years
    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:/Netw‌​ork/Library/Java/Ext‌​ensions:/System/Libr‌​ary/Java/Extensions:‌​/usr/lib/java:.
  • wh81752
    wh81752 about 10 years
    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
    wh81752 about 10 years
    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
    wh81752 about 10 years
    Please elaborate how you would call System.load() programmatically when launching Eclipse?
  • wh81752
    wh81752 about 10 years
    The question is about adding a native library (folder) on launching Eclipse.
  • wh81752
    wh81752 about 10 years
    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.
  • Philippe
    Philippe about 10 years
    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.
  • basickarl
    basickarl over 9 years
    Can I get an example.
  • anula
    anula about 9 years
    @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.
  • Alex N.
    Alex N. almost 9 years
    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.
  • Admin
    Admin about 7 years
    @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 ?
  • Aaron Digulla
    Aaron Digulla almost 7 years
    @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.