How to implement a java interface in c++?

mirleau@bluewin.ch mirleau@bluewin.ch
Wed May 17 23:30:00 GMT 2006


Hi, I'm trying to implement a java interface using c++ code,
(with the ultimate aim to do a callback from java to calling c++).

While trying, I stumbled on a full-bug-report-request
from gcj, see below. I'd be happy to enter it in
bugzilla if this issue is unknown.

Apart from the report-request, is what I'm trying supported?
How should it be done? If cni doesn't work would jni?

Here's what I did:

===========================================
[mirleau@astrid JavaFromCpp]$ gcc --version
gcc (GCC) 4.1.0 20060304 (Red Hat 4.1.0-3)
[mirleau@astrid JavaFromCpp]$ cat JavaInterface.java
public interface JavaInterface
{
    public void doIt();
}
[mirleau@astrid JavaFromCpp]$ cat gcjmain.cc
// Generic main copied from the gcj manual example
#include <gcj/cni.h>
#include <java/lang/System.h>
#include <java/lang/Throwable.h>
#include <java/io/PrintStream.h>

void myMain();

int main(int aNumberOfArguments, char* anArgumentVector)
{
  using namespace java::lang;

  try
    {
      JvCreateJavaVM(NULL);
      JvAttachCurrentThread(NULL, NULL);
      JvInitClass(&System::class$);

      myMain();

      JvDetachCurrentThread();
    }
  catch (Throwable* aThrowable)
    {
      System::err->println(JvNewStringLatin1("Unhandled Java exception:"));
      aThrowable->printStackTrace();
    }
}
[mirleau@astrid JavaFromCpp]$ cat JavaInterface.cc
#include "gcjmain.cc" // generic main
#include "JavaInterface.h" // generated by gcjh
#include <stdio.h>

class CppClass : public JavaInterface
{
public:
  void doIt()
  {
    printf("c2j2c callback\n");
  }
};

void myMain()
{
  CppClass* myObject = new CppClass();
}
[mirleau@astrid JavaFromCpp]$ make JavaInterface.exe
gcj -C JavaInterface.java
gcjh JavaInterface --classpath=.
gcj -c -fPIC JavaInterface.java
g++ -lgcj -fPIC JavaInterface.o JavaInterface.cc -o JavaInterface.exe
JavaInterface.cc: In function ?void myMain()?:
JavaInterface.cc:16: internal compiler error: can't find class$
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://bugzilla.redhat.com/bugzilla> for instructions.
Preprocessed source stored into /tmp/ccLdLfg2.out file, please attach this
to your bugreport.
make: *** [JavaInterface.exe] Error 1
rm JavaInterface.class JavaInterface.h JavaInterface.o
[mirleau@astrid JavaFromCpp]$

===================================================

Help is very much appreciated!
thanks for your time,
Olivier

P.S. I'm writing a small HowTo-Tutorial-LearnByDoing set
of examples for gcj. Does it make sense to post it
on this mailing list?




More information about the Java mailing list