This is the mail archive of the java-discuss@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

CNI invocation [was Re: Segfault on simple HelloWorld]


James CE Johnson wrote:

> Forgive the newcomer...
>
> I'm interested in using Java classes from my C++ app.  I just tripped
> across gcj a few weeks ago & started messing with it.  The simple test
> case I have below segfaults.

Hi James,

CNI so far has no "invocation interface" to allow Java code to be called
from a program with a C/C++ "main" method. The libgcj runtime assumes that
some initialization code has been run at startup (such as setting up the
java.lang.Thread environment, but there are possibly a few other things).
This initialization code only gets run from a java binary generated by gcj,
before the java programs "main" gets run.

libgcj currently creates a new thread in which to run the java "main"
method, while the initial thread simply waits and does nothing for the
lifetime of the process. It should be possible to initialize the thread
environment such that the current thread becomes the primary java thread. It
seems to me that doing this will also solve much of the invocation problem.

The interface might look something like this:

JvInitRuntime()  // Called once from C++ programs before calling into Java
code. Do any libgcj initialization stuff, and convert the current thread to
be the "main" java thread.

Of course, interactions with existing C++ code that already uses pthreads
will need to be considered. Perhaps it could simply detect if JvInitRuntime
has already been run from another thread, and if so register the current
thread as an ordinary java thread rather than going through the "main"
initialization stuff.

It shouldn't be too difficult to add this feature (patches are welcome!).
For now, you could try using JNI (supported in current libgcj snapshots), or
create a simple java "main" class to call your C++ main method, and link
your application using "gcj --main=xxx". In fact, "public static native void
main(String args[])" should probibly work ;-)

regards

  [ bryce ]



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]