Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)

share|improve this question
1  
pls see accepted answer for this question instead - for me it's much better: stackoverflow.com/questions/957700/… – am75 Jun 19 '11 at 23:03

12 Answers

up vote 17 down vote accepted

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.

share|improve this answer
2  
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
i set both...but still having trouble in finding a lib.. – nanshi Jul 25 at 7:15

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.

share|improve this answer
-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 8:38

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.

share|improve this answer
2  
How can I do it with NetBeans? – Maciek Sawicki Dec 4 '09 at 6: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 4 '09 at 8:20
How do I set it to add the .dll, in Eclipse? – NoBugs Feb 6 '12 at 22:35
@NoBugs: In Eclipse, this post should help: eclipsezone.com/eclipse/forums/t49342.html – Aaron Digulla Feb 13 '12 at 11:12

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!

share|improve this answer
1  
i did this, but still doesn't work... – nanshi Jul 25 at 7:22

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.

share|improve this answer

Like this:

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

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

share|improve this answer
Tried this idea but it resulted as java.library.path : D:\Workspace\myProject\lib;%PATH% – Touko Feb 22 '10 at 8:38
You could also use ${system_property:java.library.path} – Rob Elsner Nov 22 '10 at 23:01
The default on UNIX/Mac/GNU Linux is LD_LIBRARY_PATH. PATH is a Windows thing. – user667073 Mar 29 at 16:12
This solution may work when applied from the command line, i.e. something like eclipse.exe -vmargs Djava.library.path="C:/MyLibPath;%PATH%" .. It will not work when applied within eclipse.ini (or STS.ini) cause the core of Eclipse has no clue how to evaluate %PATH%. – user667073 Mar 29 at 16:13

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 jawin*d*.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"

share|improve this answer

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.

share|improve this answer
The thread starter was very specific about how to "append" a second native library folder, don't you agree? – user667073 Mar 29 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 at 0:27

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.

share|improve this answer
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? – user667073 Mar 29 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 3 at 15:58

I had the very same problem: I need to add (actually prepend) a native library folder to java.library.path and I had to make sure, that the default path remains intact.

This solution, based on the idear of Rob Elsner (see above), worked like a charm when used in eclipse.ini (actually STS.ini):

-Djava.library.path=my/native/folder;${system_property:java.library.path} 

According to my experience, no quotation is required. Even worse, STS did not start up with quotes (") and blanks in the path.

Hint: You must use quotes (or escape proper) when applying this solution from the command line.

share|improve this answer

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.
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.