简体   繁体   中英

Load dll library from external folder in project - Java

I'm having a problem with loading dll placed in dll folder file inside my Java code. To make it simple - I try to execute load dll inside exampleTest.java but I get UnsatisiefLinkException .

项目结构

I tried:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("../dll/file.dll");

But it doesn't work. Do anyone has any idea how to solve this issue?

dll添加到运行时类路径并将代码更改为

InputStream in = this.getClass().getClassLoader().getResourceAsStream("/dll/file.dll");

getResourceAsStream() considers your source folder as a root of the "filesystem". As far as I know you cannot access folder via getResourceAsStream() if this folder is outside of src folder.

If you want to access that dll file, move dll folder to src folder. And access it via

getResourceAsStream("/dll/file.dll")

Check this post . It is about another question, but the main point is the same I think.

If you want to load it as a ressource, you should make your dll folder into a source folder. Rightclick on folder -> Build Path -> Use as source folder

You can then simply load it like this:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("dll/file.dll");

May be try something like this.

You don't need to include the dll but the path should be enough.

System.setProperty("java.library.path", "/dll" + File.pathSeparator +
System.getProperty("java.library.path"));

Add the above line before executing your code. This will add the dll path to the jvm runtime library.

And it also depends on the folder from where you are running your application. You need to give a relative path for the dll folder. Alternatively you can give a Fully Qualified path but then it is not recommended from project configuration point of view.

If you're just wanting it imported, you could always use the System loadLibrary and nio.Paths, like this:

System.loadLibrary(Paths.get("/dll/file.dll").toString());

Which is effectively a wrapper for:

Runtime.getRuntime().loadLibrary(Paths.get("/dll/file.dll").toString())

I think you are trying to accomplish more or less what has been dealt with here: How to make a JAR file that includes DLL files?

If you try to describe better what you really want to achieve then it will be much easier to understand and give accurate answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM