invoke GCJ .o files from C/C++ program

Jeff Sturm jeff.sturm@appnet.com
Thu Jul 6 14:12:00 GMT 2000


Bryce McKinlay wrote:
> 
> Cecilia Vy-Ai Le wrote:
> 
> > I have just started to work on Java/GCJ/LIBGCJ since
> > June.
> > I build a static library which 'ar' all object files
> > generated by: gcj -c javafiles.java
> > Now, I am try to invoke this library from my C/C++
> > main program.
> 
> Using CNI, you can't currently invoke java code from a C++ program using
> CNI. This is a rather annoying deficiency and something that has been on
> the todo list of things to fix for a while.  I don't know the status of
> JNI invocation support, but I suspect it may be incomplete as well.

That's not precisely true.  CNI does not support the equivalent of JNI
invocation, i.e. the gcj runtime cannot be bootstrapped from C++ code. 
But callbacks from C++ to Java work just fine

Interestingly, you can compile/link a program that tries to circumvent
JvRunMain, and it almost works:

#include <gcj/cni.h>

#include <java/lang/Class.h>
#include <java/lang/System.h>
#include <java/io/PrintStream.h>

extern const char **_Jv_Compiler_Properties;

const char *null_properties[1] = {0};

int main(void) {
    _Jv_Compiler_Properties = null_properties;

    // Load/initialize java.lang.System class
    jclass jls = java::lang::Class::forName(
        JvNewStringLatin1("java.lang.System"));
    JvInitClass(jls);

    java::lang::System::out->println(JvNewStringLatin1("hello world"));

    return 0;
}

This program dies sometime after printing "hello world" but I honestly
didn't expect it to get that far.  Now I'm wondering if the thread
initialization, etc. couldn't be done as a side effect of class
initialization (i.e. put the startup code in Thread.<clinit>) so the
above would actually work, and JvRunMain would no longer be required?


--
Jeff Sturm
jeff.sturm@appnet.com


More information about the Java mailing list