JNI Loading a Library 2 times

H

hswerdfe

I have an old Fortran exe that runs a tank model.
I am having good success converting this Model to a dll and then loading
the dll in Java using "System.loadLibrary".

and then accessing the Fortran with a c wrapper generated with javah.exe

The Problem is that the original exe only ever had 1 instance of the
model it was running.

but I want to have 2 copies of the model it is running and be able to
pass simple information between them using Java.

But if I call System.LoadLibrary 2 or more times it just ignores the
second call and all Native calls are performed on the first and only dll
loaded.

In My Mind I see something that loads a library into memory many times.
Allows me to keep the 2 memory locations separate, And allows me to call
functions on both instances of the libraries.

I have no idea where I should start.
I am currently using Visual Studio 6.0 to create the DLL's and the 1.5 JDK



Thanks How.
 
S

Sanjay

hswerdfe said:
I have an old Fortran exe that runs a tank model.
I am having good success converting this Model to a dll and then loading
the dll in Java using "System.loadLibrary".

and then accessing the Fortran with a c wrapper generated with javah.exe

The Problem is that the original exe only ever had 1 instance of the
model it was running.

but I want to have 2 copies of the model it is running and be able to
pass simple information between them using Java.

But if I call System.LoadLibrary 2 or more times it just ignores the
second call and all Native calls are performed on the first and only dll
loaded.

In My Mind I see something that loads a library into memory many times.
Allows me to keep the 2 memory locations separate, And allows me to call
functions on both instances of the libraries.

I have no idea where I should start.
I am currently using Visual Studio 6.0 to create the DLL's and the 1.5 JDK



Thanks How.

I do not really understand why you need to load the library twice. It
would also help to show how you are loading the library. Are you using a
static block?

Besides I think loading the library in a class and then using that
class object in two different threads would also serve the same purpose.
 
E

Eric Sosman

hswerdfe wrote On 05/07/07 15:16,:
I have an old Fortran exe that runs a tank model.
I am having good success converting this Model to a dll and then loading
the dll in Java using "System.loadLibrary".

and then accessing the Fortran with a c wrapper generated with javah.exe

The Problem is that the original exe only ever had 1 instance of the
model it was running.

but I want to have 2 copies of the model it is running and be able to
pass simple information between them using Java.

But if I call System.LoadLibrary 2 or more times it just ignores the
second call and all Native calls are performed on the first and only dll
loaded.

In My Mind I see something that loads a library into memory many times.
Allows me to keep the 2 memory locations separate, And allows me to call
functions on both instances of the libraries.

I have no idea where I should start.
I am currently using Visual Studio 6.0 to create the DLL's and the 1.5 JDK

Sounds like you should turn the Fortran code into a
program rather than into a DLL. Run the program as many
times as you like, and communicate with it via input and
output streams (or other IPC mechanisms, if you prefer).
 
H

hswerdfe

Sanjay said:
I do not really understand why you need to load the library twice. It
would also help to show how you are loading the library. Are you using a
static block?

Besides I think loading the library in a class and then using that
class object in two different threads would also serve the same purpose.


I was originally using static it looked something like this

public class NativeLinker{

public native static void SetVal(double a_newVal);
public native static double GetVal();

static
{
System.loadLibrary("tankDLL");
}
}


It worked fine.
But I am now Trying to change it to something like This :

public class TankModel{
public native void SetVal_LIB(double a_newVal);
public native double GetVal_LIB();

public TankModel(){
System.loadLibrary("tankDLL");
}

public void SetVal(double d) {
SetVal_LIB(d);
}
public double GetVal() {
return GetVal_LIB();
}
}
....
//Then I Make Some Tanks , at some point
TankModel tank_A = new TankModel();
TankModel tank_B = new TankModel();

tank_A.SetVal(1.23);
tank_B.SetVal(3.21);
System.out.println(tank_A.GetVal());
System.out.println(tank_B.GetVal());


I hope this makes it clear.
I am trying to make every Instance of TankModel hold its own state in a
separate library.
The original Fortran Code was made to be a command line exe that ran and
only ever had 1 tank, and comunicated with the user with flat files.
So it only ever had to store a single tank state.

It is now a dll and We can dynamically change its current state in java.
But, I can't Get it to hold many different states.

In fact `TankModel` currently behaves exactly like`NativeLinker`, it
behaves like GetVal and SetVal are static functions.
I was hoping for a way around this..

Thanks again.

how
 
H

hswerdfe

Eric said:
hswerdfe wrote On 05/07/07 15:16,:

Sounds like you should turn the Fortran code into a
program rather than into a DLL. Run the program as many
times as you like, and communicate with it via input and
output streams (or other IPC mechanisms, if you prefer).
I am not so sure I want this.
I want to be able to communicate with the Model in a dynamic way.
I am not sure I/O streams would be efficient.

I thought the JNI was the best way to go.
I am open to other suggestions if you have some.

At any rate, the conversion to DLL is mostly already done.
and all the required access methods exist.
At this point I need a way to make several Instances of it.
 
E

Eric Sosman

hswerdfe wrote On 05/07/07 15:55,:
Eric said:
hswerdfe wrote On 05/07/07 15:16,:
[... has a library of Fortran code that models one Tank;
wants to load the library several times to model several
Tanks ...]

Sounds like you should turn the Fortran code into a
program rather than into a DLL. Run the program as many
times as you like, and communicate with it via input and
output streams (or other IPC mechanisms, if you prefer).

I am not so sure I want this.
I want to be able to communicate with the Model in a dynamic way.
I am not sure I/O streams would be efficient.

(Shrug.) Which is more efficient: Something that might
be slow, or something that doesn't work?
I thought the JNI was the best way to go.
I am open to other suggestions if you have some.

At any rate, the conversion to DLL is mostly already done.
and all the required access methods exist.
At this point I need a way to make several Instances of it.

You may have wasted your effort. Well, perhaps not
entirely wasted: Maybe you can attach the DLL to several
independent JVM instances, each handling one Tank, and use
RMI to control those Tanks from one "master" JVM. It's
really just the separate-program idea again, but using a
different communication method.

But getting one DLL attached to the same program
multiple times? And keeping all the subroutine linkages
straight, in some magical instance-specific way? I think
you're out of luck, unless you're going to write your own
native DLL loader ;-)

The Fortran code as you describe it implements what
amounts to a singleton. That's not what you'd like, but
that's the way it is. You can try to de-singularize the
singleton, or you can accept its singular nature and find
other ways to work with it.

(By the way: I don't mind receiving E-mail, but it's
confusing when the same message arrives on two separate
channels. Please send E-mail *or* post to Usenet, but
not both. Thank you.)
 
H

hswerdfe

Eric said:
hswerdfe wrote On 05/07/07 15:55,:
Eric said:
hswerdfe wrote On 05/07/07 15:16,:
[... has a library of Fortran code that models one Tank;
wants to load the library several times to model several
Tanks ...]

Sounds like you should turn the Fortran code into a
program rather than into a DLL. Run the program as many
times as you like, and communicate with it via input and
output streams (or other IPC mechanisms, if you prefer).
I am not so sure I want this.
I want to be able to communicate with the Model in a dynamic way.
I am not sure I/O streams would be efficient.

(Shrug.) Which is more efficient: Something that might
be slow, or something that doesn't work?

you make a good point.
I may eventually go this way.
The Fortran code as you describe it implements what
amounts to a singleton. That's not what you'd like, but
that's the way it is. You can try to de-singularize the
singleton, or you can accept its singular nature and find
other ways to work with it.

The 3 most obvious course of action I see are
1.) To figure out how a structure works in fortran put all the data I
need in the structure the modify all the system calls to take that
sturcture as the first argument.

2.) put everything in Java.

3.) Use your I/O Stream Idea

problem is 1 and 2 both are large rewrites. And the point of this Tank
Model is to show how to take an existing single use fortran program and
modify it to work in an easy to configure and dynamically change Java
Wrapper, and 3 might be hard to configure some of the functionality that
I need.

(By the way: I don't mind receiving E-mail, but it's
confusing when the same message arrives on two separate
channels. Please send E-mail *or* post to Usenet, but
not both. Thank you.)

Thanks for all your help, and sorry about the double post. I usually try
to keep the thread on the news group as it might be useful to somebody
else at some point. My only excuse for hitting reply all and not reply
is that the Shift Key is next to the Ctrl Key.
 
S

Sanjay

I hope this makes it clear.
I am trying to make every Instance of TankModel hold its own state in a
separate library.
The original Fortran Code was made to be a command line exe that ran and
only ever had 1 tank, and comunicated with the user with flat files.
So it only ever had to store a single tank state.

It is now a dll and We can dynamically change its current state in java.
But, I can't Get it to hold many different states.

In fact `TankModel` currently behaves exactly like`NativeLinker`, it
behaves like GetVal and SetVal are static functions.
I was hoping for a way around this..

Thanks again.

how

I don't know a way to achieve this. May be you can change your fortran
program a little.
 

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

No members online now.

Forum statistics

Threads
473,821
Messages
2,569,725
Members
45,511
Latest member
Osiris-Team

Latest Threads

Top