[NTLUG:Discuss] C++ calls Java which calls C++
Stephen Davidson
gorky at freenet.carleton.ca
Mon May 22 21:43:31 CDT 2006
Hi Steve.
Having done much JNI programming (mostly to control HW devices), I can
give you a hand, although it has been a while.
The first question I would have, is the main program accessible to the
.so library? Specifically, are the functions you are looking for in the
main program exported? Otherwise, I am not sure that the .so can see
them. If my memory serves, when the native library is loaded, it is
loaded into its own address space, and must dlopen any other libraries
that it needs. If it needs to call back into the VM, it needs to use
the *env pointer -- which may give you the handle you need.
http://java.sun.com/j2se/1.5.0/docs/index.html
http://java.sun.com/j2se/1.5.0/docs/guide/jni/index.html
http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html
Some tutorials are also available.
Q: Any reason why they can't just port the whole thing into Java? It
has been a compiled native language since 1.3 came out, over 5 years
ago. The Swing (or maybe the SWT - stay away from AWT though, unless
you a sado-masochist!) libraries are probably more than up to the task.
And if they are not, OpenGL has been available since 1.4 (about 3 years
ago). For me to port a medium size C-Module (for Serial Device Control)
into Java took about two weeks, including debugging (those command codes
and global arrays were the killers!).
If you are still stuck after this, let me know, and I will be happy to
take a closer look.
-Steve
steve wrote:
> I'm trying to add some Java scripting into a program I wrote
> ages ago (a game) so that my kid and his class of budding 9th
> grade Java progammers can use it for practicing programming
> in an environment that might be fractionally interesting to
> 15 yr olds. Bear in mind that I'm a complete newb at Java
> although I can write C++ in my sleep.
>
> What I need to do is to have the C++ game code associate a
> short Java program with each 'character' in the game - and
> give the Java code C++ functions it can call to move the
> character around...shoot...stuff like that.
>
> So, obviously I'm using JNI to create a JVM - and I have
> sucessfully loaded a number of little Java programs that
> I can call from within C++. This all works just great.
>
> Next, I need to call some C++ service functions from within
> the Java code.
>
> The only way I can seem to get this to work is to have
> the Java code load up a C++ ".so" library using
>
> System.load("libWhatever.so")
> ,..or...
> System.loadLibrary("Whatever")
>
> ...both of which load up libWhatever.so and allow me
> to call C++ functions within that library.
>
> So far, so good. The problem is that functions inside
> libWhatever.so don't seem to be able to call functions
> in the main C++ program. This is a major problem.
>
> Any ideas? What is the JVM/JNI doing when it links
> to a library that C++ wouldn't do using
> dlOpen/dlSym/dlClose?
>
>
> _______________________________________________
> http://ntlug.pmichaud.com/mailman/listinfo/discuss
>
More information about the Discuss
mailing list