Dynamic class loading

Sean McEligot smceligot@smceligot.hostings.com
Fri Apr 30 17:51:00 GMT 1999


>> While we're on the topic, what about they other way around? What are your
>> ideas on generating a native interface for a compiled object file?
>
> --Per Bothner wrote
>I don't understand the question.  What is a "native interface for a
>compiled object file"?
>
I mean compiling java into object (.o) files as opposed to .class files.
Then link it to a jni compatible library file (.so or .dll)

>Notice that compiled code has full support for reflection.  (We haven't
>implemented Method.invoke yet, but we hope to soon.)  Both compiled
>code and interpreted code will use the same reflective interface.
>
>> The hard part is getting jni to recognize java::lang::Object as a jni
>> jobject.
>
>No, it's trivial:  That's how jobject is defined.

I didn't realize that gcj has its own jni.h. I was referring to to one that
comes with the jdk.

>
>> Could the compiled object file extend both? This would be usefull
>> for native compiling a small piece of processor intensive java code
without
>> having compiling the whole program.
>
>Oh, you mean:  Could a pre-compiled class extend an interpreted class?
>No reason why not, in principle, though I don't think it makes
>much sense.
>

Not exactly.

take a java source file:

public class Simple {
  public Simple getInstance() {
    return new Simple();
  }
  public void hello() {
    System.out.println("Hello World");
  }
  }

from this create the class equivalent of:

public class Simple {
    native Simple getInstance();
    native void hello():
}

... and the object (.o) equivalent of:

// simple.cc
#include <jni.h>
#include <cni.h>
#include <Simple.h>
JNIEXPORT jobject JNICALL Java_Simple_getInstance
  (JNIEnv *, jclass) {
    Simple* gcj_simple = new Simpe();
    // return a jobject that is an instance of the Simple class with native
methods above
  }

JNIEXPORT void JNICALL Java_Simple_hello
  (JNIEnv *, jobject) {
  // get gcj_simple which is somehow mapped
  // to the jobject we returned in Java_Simple_getInstance()
    gcj_simple->hello();
}


This is rough and I haven't tried any of it yet. The idea is to run this on
a Java virtual machine with a jni library (.so, .dll) created by gcj . I
realize this is outside the purpose of gcj. Its not something I expect it to
do. I just think its an interesting idea that would be useful. If nothing
else it would allow developers (or managers) to ease into gcj without having
to compile the entire program into an executable.  I want to know if anyone
else has given this thought. Maybe I'm missing something that makes this
either much easier or nearly impossible.

Sean McEligot




More information about the Java mailing list