I don't ask much on this forum, so here comes a tough question!
I am making a program that emulates a robot using its code, which is written in C. The emulator itself is written in Java for cross-platform capability and easy GUI setup (imho). To access the robot's code, it will be compiled into a .dll along with JNI C files that allows Java to have access to its inputs, outputs, and methods used to process the inputs. Of course, the added C code will also include some files that override the assembly code that handles the input and output used on the robot controller itself.
Now, I know that a native method library can be loaded from a specific file specified at runtime with System.loadLibrary. My question is, can the library be unloaded so another set of robot code in a different library can be loaded for emulation? I already searched around a bit on Google and there is no obvious way to do this. Someone on Sun's forum found a way to unload the library, but it was unsafe and unreliable. If I cannot find out how to unload the library to load a new one, I will have to make the users of my program close it and reexecute it if they want to emulate a different set of robot code.
Loading And Unloading Jni Libraries
Started by
ham90mack
, Oct 08 2007 09:17 PM
6 replies to this topic
#1
Posted 08 October 2007 - 09:17 PM
ham90mack
http://ham90mack.googlepages.com
Resistance may be futile,
But capacitance has potential.
BLAH!
http://ham90mack.googlepages.com
Resistance may be futile,
But capacitance has potential.
BLAH!
#2
Posted 09 October 2007 - 06:12 AM
Hmm I bet that unreliable method's the closest you'll come to unloading. Will loading both not work? In other words load the first one then when you change load the second?
You might also try a local bookstore to see if there are any JNI books you could peruse that might have a solution.
You might also try a local bookstore to see if there are any JNI books you could peruse that might have a solution.
#3
Posted 09 October 2007 - 07:12 PM
Hmm... loading both will either replace the first one or throw an exception since having two libraries loaded would create a paradox... Let me look at the source code for System since I haven't actually made anything yet...
Looking at the source code of ClassLoader for the method loadLibrary and loadLibrary0, it appears that a native library is loaded into a static stack of libraries. If the library is already loaded, an exception is thrown. If two libraries that have the same name are loaded, an exception is also thrown. What I cannot see is what happens if there are two libraries that implement the same methods for the same class but they are in different files with different names and both of them are loaded. I guess I can only try later on, but it sounds like a paradoxal memory explosion would occur... (or just a simple exception) Unless I missed something in the code.
Looking at the source code of ClassLoader for the method loadLibrary and loadLibrary0, it appears that a native library is loaded into a static stack of libraries. If the library is already loaded, an exception is thrown. If two libraries that have the same name are loaded, an exception is also thrown. What I cannot see is what happens if there are two libraries that implement the same methods for the same class but they are in different files with different names and both of them are loaded. I guess I can only try later on, but it sounds like a paradoxal memory explosion would occur... (or just a simple exception) Unless I missed something in the code.
ham90mack
http://ham90mack.googlepages.com
Resistance may be futile,
But capacitance has potential.
BLAH!
http://ham90mack.googlepages.com
Resistance may be futile,
But capacitance has potential.
BLAH!
#4
Posted 14 October 2007 - 08:49 PM
Ham looks like you'll be out of luck for unloading libraries in Java. I found a request in the bug database to enable unloading and there are no plans to address it, however the bottom evaluation does give a potential work-around, write a launcher library that Java loads then in that library load/unload the real library.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4171986
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4171986
#5
Posted 15 October 2007 - 12:20 AM
I guess what is meant by the launcher library is to write a library in C that redirects native method calls to a different library that it loads. I will have to look into that...
ham90mack
http://ham90mack.googlepages.com
Resistance may be futile,
But capacitance has potential.
BLAH!
http://ham90mack.googlepages.com
Resistance may be futile,
But capacitance has potential.
BLAH!
#6
Posted 14 September 2008 - 03:46 PM
Check out this tutorial. Everything about unloading native libraries is explained here: http://codethesis.com/tutorial.php?id=1
#7
Posted 14 September 2008 - 06:03 PM
Hmm... Although I quit that project long ago due to time constraints, this is interesting information. I will bookmark it if I will ever need it later. Thanks!
ham90mack
http://ham90mack.googlepages.com
Resistance may be futile,
But capacitance has potential.
BLAH!
http://ham90mack.googlepages.com
Resistance may be futile,
But capacitance has potential.
BLAH!











