How to set the java.library.path from Eclipse
Asked Answered
M

16

217

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.

Mim answered 5/6, 2009 at 19:27 Comment(1)
I've done it by adding it as a VM argument and it did work. How exactly did you do it?Mistrustful
G
301

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

Grader answered 5/6, 2009 at 20:50 Comment(14)
OK this seems to work. Now the right path is actual shown in the error message. required library not found : /Users/janusz/Documents/workspace/SlideSelector/lib/libOpenCV.jnilib. The file is in exactly this directory. Seems to be another errorMim
But how does this work if you have more than one folder to add?Shifflett
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?Grader
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.Shifflett
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.Grader
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 SR2Ralf
@Ralf I fall into same problem .Did you found solution?Shortchange
@MyTitle no, but we worked around it - we switched to using the mavennatives plugin, which magically sets it for you. Worth exploring if you already use maven. This was a little more satisfying overall since everyone on my team can share the setting more effectively.Ralf
Unfortunately, Eclipse isn't smart enough to correctly map everything the same way when you export an executable jar.Footworn
java -classpath .:/usr/local/share/java/*.jar -Djava.library.path=/usr/local/lib HelloWorldReni
@Ralf Preferences not sticking can be indicative of read only properties files, e.g. your eclipse project/metadeta/classpath files may be in SCM and need to be opened for editing.Fought
What if I don't depend on a jar. I just want to load a dll.Plautus
@Fought My preferences are not sticking either. I am trying to set the native library of one of my maven dependencies(gurobi). My project is under gits version control. Please can you elaborate on how I can make the preferences stick?Borneo
@Borneo I'm a bit out of the context of this issue now, but you should look for eclipse .project or .metadata files that are checked into git. If they are managed by git, then they will be marked as read-only by the file system and even though you change the preference in eclipse the file system will reject the change when eclipse tries to save it. Remove your eclipse files from git or open them for edit.Fought
R
39

If you are adding it as a VM argument, make sure you prefix it with -D:

-Djava.library.path=blahblahblah...
Rosenberg answered 5/6, 2009 at 19:43 Comment(1)
See this post: https://mcmap.net/q/128338/-how-to-add-native-library-to-quot-java-library-path-quot-with-eclipse-launch-instead-of-overriding-it. -Djava.library.path="${workspace_loc:project}\lib;${env_var:PATH}"Retirement
D
24

Except the way described in the approved answer, there's another way if you have single native libs in your project.

  • in Project properties->Java Build Path->Tab "Source" there's a list of your source-folders
  • For each entry, there's "Native library locations", which also supports paths within the workspace.
  • This will make Eclipse add it to your java.library.path.
Demand answered 4/8, 2011 at 10:43 Comment(1)
awesome!!!! thanks a lot!!! that resolved my issue. needed to add dylib to my project and a jar. setting native library location fixed it!Bashan
H
12

For a given application launch, you can do it as jim says.

If you want to set it for the entire workspace, you can also set it under

Window->
  Preferences->
    Java->
      Installed JREs

Each JRE has a "Default VM arguments" (which I believe are completely ignored if any VM args are set for a run configuration.)

You could even set up different JRE/JDKs with different parameters and have some projects use one, other projects use another.

Hanaper answered 5/6, 2009 at 20:50 Comment(0)
D
8

You can simply add -Djava.library.path=yourPath to the eclipse.ini.

Damaris answered 24/4, 2011 at 10:50 Comment(0)
W
6

Just add the *.dll files to your c:/windows

You can get the java.library.path from the follow codes:and then add you dll files under any path of you get

import java.util.logging.Logger;

public class Test {


    static Logger logger = Logger.getLogger(Test.class.getName());
    public static void main(String[] args) {
    logger.info(System.getProperty("java.library.path"));
    }
}
Wouldst answered 3/5, 2011 at 1:49 Comment(2)
This would mean that everyone using your project would also need to put them there.Lamdin
Thanks for this. The idea of putting the DLLs into c:/windows proved to me that it was just a path issue and not a problem with the DLLs.Thilde
L
5

I think there is another reason for wanting to set java.library.path. Subversion comes with many libraries and Eclipse won't see these unless java.library.path can be appended. For example I'm on OS-X, so the libraries are under \opt\subversion\lib. There are a lot of them and I'd like to keep them where they are (not copy them into a standard lib directory).

Project settings won't fix this.

Libeler answered 9/9, 2011 at 23:55 Comment(1)
then, where do I need to add the java.library.path parameters?Toth
K
5

None of the solutions above worked for me (Eclipse Juno with JDK 1.7_015). Java could only find the libraries when I moved them from project_folder/lib to project_folder.

Karim answered 13/5, 2013 at 22:49 Comment(1)
What? Why should that be important..? O_oMarenmarena
R
4

Click Run
Click Debug ...
New Java Application
Click Arguments tab
in the 2nd box (VM Arguments) add the -D entry

-Xdebug -verbose:gc -Xbootclasspath/p:jar/vbjorb.jar;jar/oracle9.jar;classes;jar/mq.jar;jar/xml4j.jar -classpath -DORBInitRef=NameService=iioploc://10.101.2.94:8092/NameService  

etc...

Rodina answered 5/6, 2009 at 19:56 Comment(1)
Also - in the run configuration, go to the "Common" tab and choose a place in your project to save the run config. This allows you to check in that run configuration (xxxx.launch file) so that others on your team can reuse it.Hanaper
C
3

Another solution would be to open the 'run configuration' and then in the 'Environment' tab, set the couple {Path,Value}.

For instance to add a 'lib' directory located at the root of the project,

    Path  <-  ${workspace_loc:name_of_the_project}\lib
Cornute answered 23/1, 2013 at 10:3 Comment(1)
But maybe directly the VM argument -Djava.library.path in the Arguments tab?Marenmarena
O
1

Remember to include the native library folder in PATH.

Orfinger answered 19/2, 2014 at 13:46 Comment(0)
C
1

I'm using Mac OS X Yosemite and Netbeans 8.02, I got the same error and the simple solution I have found is like above, this is useful when you need to include native library in the project. So do the next for Netbeans:

1.- Right click on the Project
2.- Properties
3.- Click on RUN
4.- VM Options: java -Djava.library.path="your_path"
5.- for example in my case: java -Djava.library.path=</Users/Lexynux/NetBeansProjects/NAO/libs>
6.- Ok

I hope it could be useful for someone. The link where I found the solution is here: java.library.path – What is it and how to use

Cayman answered 31/3, 2015 at 2:53 Comment(0)
T
0

Sometime we dont get Java Build Path by directly right click on project. then go to properties.... Right Click and go to properties

Then Click on java build pathProperties Scrren

Click on add external jar

Click on tab add external jars and give path of your computer file where u have stored jars.

Tiler answered 11/7, 2013 at 12:38 Comment(0)
T
0

Here is another fix:

My build system (Gradle) added a required native library (dll) to the Eclipse build path (Right Click on Project -> Properties -> Java Build Path -> Libraries). Telling the build system not to add the native dll library to the Eclipse classpath solved the problem.

Tactical answered 11/3, 2015 at 14:38 Comment(0)
P
0

You can add vm argument in your Eclipse.

Example :

-Djava.ext.dirs=cots_lib

where cots_lib is your external folder library.

Pleading answered 18/1, 2018 at 4:24 Comment(0)
M
-1

the easiest way would to use the eclipse IDE itself. Go to the menu and set build path. Make it point to the JAVA JDK and JRE file path in your directory. afterwards you can check the build path where compiled files are going to be set. in the bin folder by default though. The best thing would be to allow eclipse to handle itself the build path and only to edit it similar to the solution that is given above

Mcclary answered 21/4, 2014 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.