This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Re: How to implement a java interface in c++?
- From: mirleau at bluewin dot ch
- To: java at gcc dot gnu dot org
- Date: Thu, 18 May 2006 11:06:36 +0200
- Subject: Re: How to implement a java interface in c++?
>The only way I see for you is the subclass in java code
>and impement all methods of that class in C++. Thats easily possible
>with CNI. And it should be fast.
>
>
>Michael
Hi, thanks for your suggestion, I'd like to try it, but I'm not
expert enough to do what I think you meant. Here's what I did:
I wrote a java subclass:
====CppImplementation.java========================
public class CppImplementation implements JavaInterface
{
public void doIt(){int a = 1;}
}
=====================================
And used gcjh to generate a header file:
====CppImplementation.h=================================
// DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-
#ifndef __CppImplementation__
#define __CppImplementation__
#pragma interface
#include <java/lang/Object.h>
extern "Java"
{
class CppImplementation;
};
class ::CppImplementation : public ::java::lang::Object
{
public:
virtual void doIt ();
CppImplementation ();
static ::java::lang::Class class$;
};
#endif /* __CppImplementation__ */
==============================================
The way I interpret your answer is that I could now forget the
java class and just directly implement this header file in c++.
I'm not certain on how to do that. I guess I need to use
gcj conventions to correctly implement it.
For example I was playing around by in-/out commenting some lines
below, but that didn't work out link-wise: (vtables and Object::Object()):
============ CppImplementation.cc =================
#include "CppImplementation.h" // generated by gcjh
// #pragma interface
#include <java/lang/Object.h>
//extern "Java"
//{
// class CppImplementation;
//};
void CppImplementation::doIt (){};
CppImplementation::CppImplementation (){};
::java::lang::Class CppImplementation::class$;
===================================================
Is there an example around where it says how to implement such
a generated header file yourself?
By the way is there a pdf version of the gcj manual somewhere?
Or actually a document that I can print in one go?
Thanks, Olivier