How to implement a java interface in c++?

David Daney ddaney@avtrex.com
Wed May 17 23:36:00 GMT 2006


mirleau@bluewin.ch wrote:
> 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

You cannot extend a java class in C++.  You will probably have to change 
your architecture so that you don't do this.

The CNI C++ class hierarchy comes from the java code.  It is not 
supported to extend it with pure C++ code.


David Daney



More information about the Java mailing list