Other B4J SLC how to include native libraries

peter01

Member
Licensed User
Longtime User
How can I set the native library path for the B4J SLC, so that the created jar Library can refer to my included native dll file (in Windows). In B4J I get an java.lang.UnsatisfiedLinkError: no jMath (my dll) in java.library.path error. The jMath.dll is a c library file using the Java jni functions.
In Eclipse everything works with the include path for my jMath folder: C:\eclipse\workspace\jMath\additional for the native library location. I inspected the created jar file with 7-zip. the dll is in the root and the jMath class in the com/peter/jmath folder as specified in my java file.
 

peter01

Member
Licensed User
Longtime User
The dll is loaded with :
static {
System.loadLibrary("jMath");
}
This seems to be the problem, because for System.loadLibrary() the dll has to be in the Java Library path. As far as I know eclipse does this with with the include path for the native library folder, in this case : C:\eclipse\workspace\jMath\additional. So it works with eclipse. And SLC uses the additional path to include libraries. So the solution would be to have a relative path in the Java code. But how to do that?
 

peter01

Member
Licensed User
Longtime User
But then I have the problem, that the B4J SLC will not include my dll, because the dll is not in the additional folder.
 

peter01

Member
Licensed User
Longtime User
I found one possible solution for this problem. Using: System.load("C:/B4J/Libs added/jMath.dll") and placing the dll in the /Libs added folder works with B4J SLC. But it needs this /Libs added folder with the dll even with the B4J packager installation. Another possibility could be to use System.loadLibrary("jMath") and add for example C:/B4J/Libs added/ to the library path. But I don't know if the B4J packager can handle this?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
But then I have the problem, that the B4J SLC will not include my dll, because the dll is not in the additional folder.
The dll shouldn't be inside the jar.

1. Add it to the Assets files (Files tab).
2. Copy it to File.DirData when the program starts.
3. Change the library code and add a method that loads the library from the path you pass from the B4J code.
 

peter01

Member
Licensed User
Longtime User
Yes, this works great - I implemented a init routine to load the dll from the File.DirData. Then everythings is running!!
Thank you very much for your help!! :)
 
Top