Is it possible to loadlibrary() two different libs for the "same" java class?

J

John

Hi,

I have 20 different algorithms that do different things, but have the
same interface. I need to be able to call these algorithms from Java.

I built a Java (JNI) interface for the algorithms. In my app I want to
do something like

public class Alg
{
// native interface functions here

public Alg(string libraryname)
{
System.loadlibrary(libraryname);
}
}

main()
{
Alg alg1 = new Alg("alg1");
Alg alg2 = new Alg("alg2");
}

When I do this, only the first algorithm (alg1) in this case is loaded.

What's the solution?

THANK YOU!!!!
John
 
C

Chris Uppal

John said:
I have 20 different algorithms that do different things, but have the
same interface.

So you'd expect to have 20 different subclasses of a common abstract base-class
(or interface) that defines their common interface. This is nothing to do
with JNI, it's just how OO is supposed to work.

If you need to use Strings to decide which algorithm to use (say, to make the
choice of algorithm externally configurable), then you could use a Map from
Strings to some object connected with the algorithm. E.g. if the algorithm
objects are stateless, then you could use a Map<String, AbstractAlgorithm> that
was populated with canonical instances. If the algorithm objects cannot be
re-used, then you could just copy the object found in the Map. Alternatively
you could use Class.forName(). There are many ways of organising this kind of
lookup with different tradeoffs between flexibility and convenience. The
important point, though, is that setting up the correct structure is not a JNI
issue.

Once you have the structure/framework in place, using JNI to implement the 20
separate concrete classes should be straightforward. (Or as straightforward as
it's ever going to get ;-) You /could/ use 20 different libs, but I suspect
that it would be easier to make the 20 Java classes share the same lib with
each using one of 20 different C/C++ function names.

-- chris
 
G

Gordon Beaton

I have 20 different algorithms that do different things, but have the
same interface. I need to be able to call these algorithms from Java.

I built a Java (JNI) interface for the algorithms. In my app I want to
do something like

public class Alg
{
// native interface functions here

public Alg(string libraryname)
{
System.loadlibrary(libraryname);
}
}

main()
{
Alg alg1 = new Alg("alg1");
Alg alg2 = new Alg("alg2");
}

When I do this, only the first algorithm (alg1) in this case is loaded.

What's the solution?

You can't assign a different set of methods per object instance using
JNI, just like you can't in Java.

Since your algorithms are in native code, you can work around this by
invoking object-specific methods with a switch or through a set of
stored function pointers in the native methods themselves, but it is a
complicated way to solve this.

You should define an Interface and create classes for each of the
algorithms. If you want to hide such implementation details from the
user, either use a factory method in Alg, or instansiate a delegate
object (according to the user-specified type) in each Alg instance,
and pass your method calls through that.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top