BridJ Loading library confusion

311 views
Skip to first unread message

Supernatural

unread,
Jun 1, 2012, 9:20:51 AM6/1/12
to NativeLibs4Java
Hello,

Here is the code I used -

@Runtime(CPPRuntime.class)
public class TestLibrary1 {

static
{
//Do we need anything here?
}

public static void LoadLibrary(String path)
{
File test = new File("C:\\MyFolder\\"); //Folder where the
DLL is
BridJ.setNativeLibraryFile("mynativelib.dll", test); //Set
the DLL and name
BridJ.register(); //Register TestLibrary for native calls
}

//JNA Native methods from mynativelib.DLL

}


As you see the LoadLibrary is used to load the library at runtime. But
these calls are successful, but when there is a call to actual native
methos in mynativelib.DLL it gives me "java.lang.UnsatisfiedLinkError"

How to load the library dynamically? I am confused !

Olivier Chafik

unread,
Jun 1, 2012, 12:09:12 PM6/1/12
to nativel...@googlegroups.com
Hello,

You need to add a @Library("mynativelib") annotation so that
BridJ.register() knows what library your class should be bound to.

Also, setNativeLibraryFile doesn't work the way you're using it : it
takes the name of the library (without the ".dll" suffix) and the
exact file (not the directory), as in :

BridJ.setNativeLibraryFile("mynativelib", "c:\\my\\path\\mynativelib.dll");

Probably what you might want to do here is use
BridJ.addLibraryPath("c:\\my\\path"); instead of setLibraryName.

Please also refer to the wiki for library lookup reference :

https://code.google.com/p/bridj/wiki/LibrariesLookup

Cheers
--
Olivier
http://ochafik.com
http://twitter.com/ochafik

2012/6/1 Supernatural <amitvp...@gmail.com>:

Supernatural

unread,
Jun 4, 2012, 10:07:02 AM6/4/12
to NativeLibs4Java
Thanks Oliver. It worked. Cool !

Now what I did was to copy the same DLL in several folders - F1, F2
and F3 and used the same BridJ TestLibrary1. I was wondering if it
will load three different instances of the same copy of DLL. On
Windows it returns a separate DLL handle by calling LoadLibrary and
pass a different folder path to it

The objective here is that I want to use the same TestLibrary1 since
the functions exposed are same across the DLLs

So what I did is this -

main() //in main
{
lib1 = new TestLibray1();
lib2 = new TestLibray1();
lib3 = new TestLibray1();

lib1.LoadLibrary("Library1", "C:\\F1\\TestLibrary1.DLL"); //Same DLL
but in different folders so that its as good as different DLL
lib2.LoadLibrary("Library2", "C:\\F2\\TestLibrary1.DLL");
lib3.LoadLibrary("Library3", "C:\\F3\\TestLibrary1.DLL");
}

//Inside TestLibrary1
static
{
BridJ.Register();
}

Small change - LoadLibrary now also accepts library name and the path.
So now my new LoadLibrary function looks like this -

public void LoadLibrary(String LibName, String path)
{
File libFile = BridJ.getNativeLibraryFile(path); //This should
create a new library handle
BridJ.setNativeLibraryFile(LibName, libFile); //This will add to
a new entry in the map
}

But it uses the BridJ configuration only for the first LoadLibrary.
Rest it seems to ignore or not seems to be in effect

Olivier Chafik

unread,
Jun 5, 2012, 3:59:43 PM6/5/12
to nativel...@googlegroups.com
Hi Amit,

2012/6/4 Supernatural <amitvp...@gmail.com>:
> Thanks Oliver. It worked.  Cool !

No pb :-)

> Small change - LoadLibrary now also accepts library name and the path.
> So now my new LoadLibrary function looks like this -
>
> public void LoadLibrary(String LibName, String path)
> {
>      File libFile = BridJ.getNativeLibraryFile(path); //This should
> create a new library handle
>      BridJ.setNativeLibraryFile(LibName, libFile); //This will add to
> a new entry in the map
> }
>
> But it uses the BridJ configuration only for the first LoadLibrary.
> Rest it seems to ignore or not seems to be in effect

The problem here is that you only have one library binding class. The
way JNI works, it is not possible to bind a given native method to
different native functions depending on the instance of your
TestLibrary1 class.

Also, the name declared by TestLibrary1's @Library annotation is the
one that will be used to perform the binding.

The only easy way to bind three identical libraries, in the current
state of BridJ, is to copy-paste your TestLibrary1 class for each copy
of the library, adjusting the name in the @Library annotation, and
just associate each name to its path.

Out of curiosity, why are you trying to load the library three times ?

Supernatural

unread,
Jun 6, 2012, 5:50:05 AM6/6/12
to NativeLibs4Java
The library has a static/configuration data on which the native
functions work. This data is nothing but a Target (PCB/hardware)
configuration.

Basically there are multiple user interface configurations - for each
such configuration we must attach a DLL. There is no limit on number
of such configurations. Every time user adds configuration we attach a
DLL to it (since it will have a seprate DLL configuration)

I know it is really poor design. But the DLL is third party. To add to
issues there is no limit on number of DLLs loaded at a time. So you
can see that all has to be done at runtime. :(


milo...@gmail.com

unread,
Nov 6, 2018, 9:11:47 AM11/6/18
to NativeLibs4Java
Sorry for resuming this old thread but I have exactly the same problem. 
Any new idea about better solutions, i.e. due to improvements in newer Bridj versions?
Reply all
Reply to author
Forward
0 new messages