ChatGPT解决这个技术问题 Extra ChatGPT

How to set the java.library.path from Eclipse

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.

I've done it by adding it as a VM argument and it did work. How exactly did you do it?

C
Campa

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.

https://i.stack.imgur.com/zeFNt.png


But how does this work if you have more than one folder to add?
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?
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.
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.
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 SR2
m
matt b

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

-Djava.library.path=blahblahblah...

See this post: stackoverflow.com/a/2309723/510583. -Djava.library.path="${workspace_loc:project}\lib;${env_var:PATH}"
B
BuZZ-dEE

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.


awesome!!!! thanks a lot!!! that resolved my issue. needed to add dylib to my project and a jar. setting native library location fixed it!
S
Scott Stanchfield

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.


C
CoolBeans

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


B
BuZZ-dEE

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"));
    }
}

This would mean that everyone using your project would also need to put them there.
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.
G
Günter

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.


What? Why should that be important..? O_o
B
BuZZ-dEE

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.


then, where do I need to add the java.library.path parameters?
J
Janusz

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...


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.
A
Aurelien

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

But maybe directly the VM argument -Djava.library.path in the Arguments tab?
J
JuanFran Adame

Remember to include the native library folder in PATH.


a
alexventuraio

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


v
vardhaman

https://i.stack.imgur.com/ewKXd.png

https://i.stack.imgur.com/u5gqH.png

https://i.stack.imgur.com/HwIOT.png

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


m
marc

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.


B
BuZZ-dEE

You can add vm argument in your Eclipse.

Example :

-Djava.ext.dirs=cots_lib

where cots_lib is your external folder library.


k
khangulmalik

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