Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

JNI Loading a Library 2 times

Thread view: 
hswerdfe - 07 May 2007 20:16 GMT
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.
Sanjay - 07 May 2007 20:31 GMT
> 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
[quoted text clipped - 20 lines]
>
> 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.
hswerdfe - 07 May 2007 20:46 GMT
>> I have an old Fortran exe that runs a tank model.
>> I am having good success converting this Model to a dll and then
[quoted text clipped - 28 lines]
> 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
Sanjay - 08 May 2007 05:33 GMT
> I hope this makes it clear.
> I am trying to make every Instance of TankModel hold its own state in a
[quoted text clipped - 13 lines]
>
> how

I don't know a way to achieve this. May be you can change your fortran
program a little.
Eric Sosman - 07 May 2007 20:35 GMT
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
[quoted text clipped - 18 lines]
> 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).

Signature

Eric.Sosman@sun.com

hswerdfe - 07 May 2007 20:55 GMT
> hswerdfe wrote On 05/07/07 15:16,:
>> I have an old Fortran exe that runs a tank model.
[quoted text clipped - 24 lines]
> 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.
Eric Sosman - 07 May 2007 22:36 GMT
hswerdfe wrote On 05/07/07 15:55,:

>>hswerdfe wrote On 05/07/07 15:16,:
>>[... has a library of Fortran code that models one Tank;
[quoted text clipped - 9 lines]
> 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.)

Signature

Eric.Sosman@sun.com

hswerdfe - 08 May 2007 00:33 GMT
> hswerdfe wrote On 05/07/07 15:55,:
>>
[quoted text clipped - 14 lines]
>     (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.

>> I thought the JNI was the best way to go.
>> I am open to other suggestions if you have some.
[quoted text clipped - 8 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.