undefined reference to vtable

Shaun Jackman sjackman@telus.net
Wed Aug 11 16:52:00 GMT 2004


I was hoping to show that it's possible to use a shared library
implemented in Java from an entirely C++ program. This is very nearly
true. From what I see though, you cannot implement a new Java class
entirely in C++, which is necessary for example to create a small
callback (Listener) class. I'd like to make a feature request for the
ability to write Java classes entirely in C++.
For those interested, following this e-mail is a short HelloWorld
application that uses SWT (a Java GUI library) writen in 99% C++.
Cheers,
Shaun
On Tue August 10, 2004 18h45, Bryce McKinlay wrote:
> You're trying to define a Java class in C++. You can't do that because 
> only gcj knows to generate the java.lang.Class object and other metadata 
> for the class. You need to declare ShellSelectionListener in a .java 
> file with "native" methods, and then implement those methods in C++.
>> Regards
>> Bryce

[HelloWorld.cc]
#include "OkSelectionListener.h"
#include <org/eclipse/swt/SWT.h>
#include <org/eclipse/swt/events/SelectionEvent.h>
#include <org/eclipse/swt/events/SelectionListener.h>
#include <org/eclipse/swt/layout/RowLayout.h>
#include <org/eclipse/swt/widgets/Button.h>
#include <org/eclipse/swt/widgets/Display.h>
#include <org/eclipse/swt/widgets/Label.h>
#include <org/eclipse/swt/widgets/Menu.h>
#include <org/eclipse/swt/widgets/Shell.h>
#include <gcj/cni.h>
using namespace org::eclipse::swt;
using namespace org::eclipse::swt::events;
using namespace org::eclipse::swt::layout;
using namespace org::eclipse::swt::widgets;
using namespace java::lang;
/** Main shell. */
static Shell* shell;
/** Closes the shell. */
void OkSelectionListener::
widgetSelected( SelectionEvent*)
{
	shell->close();
}
/** Displays "Hello, world!" using SWT. */
int
main()
{
	JvCreateJavaVM( NULL);
	JvAttachCurrentThread( NULL, NULL);
	Display* display = new Display;
	shell = new Shell( display);
	shell->setText( JvNewStringUTF( "Hello"));
	Menu* menu = new Menu( shell, SWT::BAR);
	shell->setMenuBar( menu);
	Label* label = new Label( shell, SWT::NONE);
	label->setText( JvNewStringUTF( "Hello, world!"));
	Button* button = new Button( shell, SWT::PUSH);
	button->setText( JvNewStringUTF( "Ok"));
	SelectionListener* listener = (SelectionListener*)new OkSelectionListener;
	button->addSelectionListener( listener);
	RowLayout* rowLayout = new RowLayout();
	rowLayout->type = SWT::VERTICAL;
	shell->setLayout( rowLayout);
	shell->open();
	while( !shell->isDisposed()) {
		if( !display->readAndDispatch())
			display->sleep();
	}
	display->dispose();
	JvDetachCurrentThread();
	return 0;
}
[OkSelectionListener.java]
import org.eclipse.swt.events.*;
class OkSelectionListener extends SelectionAdapter
{
 public native void widgetSelected( SelectionEvent event);
}


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /