Hello, i made a little HelloWorld C++ executable to popup a Java HelloWolrd GUI, but it gives a core dump. My environment: ------------ - Pentium Linux 2.0.36 with glibc - Motif 2.0.1 (shared library) - jdk1.2pre-v1.tar from Blackdown The output: (after invoking the C++ executable HelloWorldAwt) ------------------------------------------------ .... [Loaded sun.awt.NativeLibLoader from /usr/local/jdk1.2Pre1/jre/lib/rt.jar] [Dynamic-linking native method sun/awt/PlatformFont.initIDs ... JNI] [Loaded sun.awt.PlatformFont$1 from /usr/local/jdk1.2Pre1/jre/lib/rt.jar] <GC: managing allocation failure: need 1032 bytes, type=1, action=1> .... [Dynamic-linking native method sun/awt/FontDescriptor.initIDs ... JNI] [Loaded sun.awt.motif.CharToByteX11Dingbats from /usr/local/jdk1.2Pre1/jre/lib/rt.jar] [Loaded sun.awt.CharToByteSymbol from /usr/local/jdk1.2Pre1/jre/lib/rt.jar] SIGSEGV 11* segmentation violation stackpointer=0xbfffeba0 Full thread dump Classic VM (Linux_JDK_1.2_pre-release-v1, native threads): "AWT-Motif" (TID:0x412c11a8, sys_thread_t:0x81be900, state:MW, native ID:0x1806) prio=5 at sun.awt.motif.MToolkit.run(Native Method) at java.lang.Thread.run(Thread.java:479) "SunToolkit.PostEventQueue-0" (TID:0x412c1010, sys_thread_t:0x81a2c30, state:CW, native ID:0x1405) prio=5 at java.lang.Object.wait(Native Method) .... How to reproduce: ------------- There are 3 little files attached: HelloWorldAwt.C The C++ executable HelloWorldAwt.java The AWT Gui Popup window Makefile How i compile the stuff. Question: ------- Did anybody try this before? Which Motif version do I need? Thanks for any hints, Marcel PS: The code seems to be more or less correct, since i just run it on Windows NT successfully
/* HelloWorldAwt.C */ #include <jni.h> #include <iostream> using namespace std; bool done = false; int main() { const int nOpts = 4; JavaVMOption options[nOpts]; int ii=0; options[ii++].optionString = "-Djava.compiler=NONE"; /* disable JIT */ options[ii++].optionString = "-Djava.class.path=."; /* user classes */ options[ii++].optionString = "-Djava.library.path=."; /* set native library path */ options[ii++].optionString = "-verbose:jni,class,gc"; /* print JNI-related messages */ JavaVMInitArgs vm_args; vm_args.version = JNI_VERSION_1_2; vm_args.options = options; vm_args.nOptions = nOpts; vm_args.ignoreUnrecognized = JNI_TRUE; JavaVM *jvm; JNIEnv *env; jint res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args); if (res < 0) { cerr << "Can't create Java VM" << endl; exit(1); } cout << "JNI_CreateJavaVM() is OK" << endl; jclass cls = env->FindClass("HelloWorldAwt"); if (cls == 0) { cerr << "Can't find HelloWorldAwt class" << endl; exit(1); } jmethodID cons = env->GetMethodID(cls, "<init>", "()V"); // Constructor if (cons == 0) { cerr << "Constructor ERROR" << endl; exit(1); } jobject obj = env->NewObject(cls, cons); if (obj == 0) { cerr << "NewObject ERROR" << endl; exit(1); } cout << "HelloWorldAwt.C: Entering forever loop ..." << endl; while (!done) { } cout << "HelloWorldAwt.C: Good bye ..." << endl; jvm->DestroyJavaVM(); return 0; }
#JDK_HOME=/usr/local/jdk MY_JAVAC = javac MY_INCLUDE = -I$(JDK_HOME)/include -I$(JDK_HOME)/include/linux MY_JAVA_LIBS= -L$(JDK_HOME)/jre/lib/i386 -ljava -lawt -L $(JDK_HOME)/jre/lib/i386/native_threads -lhpi -L$(JDK_HOME)/jre/lib/i386/classic -ljvm MY_X_LIBS = -L/usr/X11R6/lib -lXm -lXpm -lXt -lSM -lICE -lXext -lX11 HelloWorldAwt.class : HelloWorldAwt.java $(MY_JAVAC) HelloWorldAwt.java @echo "-> HelloWorldAwt.class done" @echo "" HelloWorldAwt.h : HelloWorldAwt.java javah -jni HelloWorldAwt @echo "-> HelloWorldAwt.h done" @echo "" HelloWorldAwt : HelloWorldAwt.class HelloWorldAwt.h HelloWorldAwt.C g++ -D_REENTRANT $(MY_INCLUDE) $(MY_JAVA_LIBS) $(MY_X_LIBS) HelloWorldAwt.C -o HelloWorldAwt @echo "-> HelloWorldAwt (C++ executable) done" @echo "" all: usage HelloWorldAwt @echo "######### Starting C++ application with Awt GUI ... ########" HelloWorldAwt @echo "" clean: rm -f *.class *.so rm -f HelloWorldAwt rm -f HelloWorldAwt.h usage: @echo "---------------------------------------------------------" @echo "CAUTION: Set this environment before using the Makefile! " @echo "export LD_LIBRARY_PATH=.:$(JDK_HOME)/jre/lib/i386:$(JDK_HOME)/jre/lib/i386/native_threads:$(JDK_HOME)/jre/lib/i386/classic" @echo "export LD_BIND_NOW=true" @echo "export RTLD_NOW=true" @echo "export LD_PRELOAD=libpthread.so" @echo "---------------------------------------------------------"
/* HelloWorldAwt.java */ import java.awt.*; import java.awt.event.*; public class HelloWorldAwt extends Frame { Toolkit toolkit = Toolkit.getDefaultToolkit(); Button beepButton; private String myName = "HelloWorldAwt.java: "; public HelloWorldAwt() { System.out.println(myName + "Entering constuctor ..."); setTitle("My little JNI-JDK-2 test"); init(); setSize(400, 200); show(); System.out.println(myName + "Leaving constuctor ..."); } public void init() { System.out.println(myName + "Entering init() ..."); beepButton = new Button("Hi you - Beep me :-)"); class BeepListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(myName + "Entering actionPerformed() ..."); toolkit.beep(); } } beepButton.addActionListener(new BeepListener()); setLayout(new FlowLayout()); add(beepButton); } /** Use this for testing the java AWT <br> java HelloWorldAwt */ static public void main(String[] args) { HelloWorldAwt aa = new HelloWorldAwt(); //aa.showWindow(); } }