B4J Question JNI System.LoadLibrary(libName)

agb2008

Member
Licensed User
Longtime User
I am trying to use a java library (converted using SimpleLibraryCompiler) which is a wrapper around .dll native library. Got application example in .java where first of all there a call to System.loadLibrary(libName) where libName is name of used library.dll (right now I am talking about solution for Win platform). But how could I implememnt such call in B4J ? Should I extend existing .java library and add simple class that would load such library ? But if I do so - how could I control where to load such library from ? Any advice ?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
This is how I incorporate dlls into B4J libraries using NetBeans. The process should be similar in Eclipse.

Write your Java code. There should not be a main class. It should just be a bunch of non-main classes. One of your classes will load the dll (most likely in its no-argument constructor; note that the no-arg constructor is called when, in B4J, you Dim an object of that class) using System.LoadLibrary if you are using JNI or Native.LoadLibrary if you are using JNA. I recommend using JNA. Check out JNA's GitHub page for some code examples and the required jna.jar.

In your Projects window in NetBeans, right click on your project and choose New>Folder. Create a new folder called "res". Then, right-click on Projects again and choose Properties. In the Sources section, add a new folder so that your new "res" folder is the "Package Folder" and it is labeled "Resources". Then, using a file explorer, copy and paste your dll int the "res" folder. Ideally, you'll have a 64-bit dll that will go under "res/win32-x86-64" and a 32-bit version of that dll that will go under "res/win32-x86". JNA will load the correct dll depending on the bitness of the JVM in which it finds itself running. I think you have to manage that yourself if you are using JNI.

Then, run your project through SimpleLibraryCompiler. This will only be useful for generating the xml file. It will create a jar file but if you unzip that jar, you'll see that your dlls aren't included. To remedy that, build (don't run) your project in NetBeans. In NetBeans, it's a hammer icon next to the run button. You can also build it from the "Run" menu item. It's probably similar in Eclipse. In your NetBeans project folder, you'll see a sub folder called "dist". This will contain your compiled jar. If you unzip it, you'll find the dlls were included. Copy this jar to your B4J external libraries folder and make sure it's named correctly.

This method will allow you to create jars with B4J that have the required dlls included.
 
Upvote 0
Top