Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

creating JFrame from C via JNI



I am trying to launch a Java program with a GUI front end from a C program.
The launching, locating, and calling of methods is working fine, but creating
a JFrame is causing an error that I cannot find enough documentation on:


JavaAWT: NSException not handled by native method. Passing to Java.
java.lang.RuntimeException: Non-Java exception raised, not handled!
(Original problem: Error (1002) creating CGSWindow)
at apple.awt.OSXOffScreenSurfaceData._copyNSImagePixels (Native Method)


My C program is below. It runs on Linux, but my Mac attempts are frustrated.
Here is the Linux compilation:
gcc
-D_REENTRANT
-I $JDK/linux
-I $JDK/linux/include
GUIdooey.c
-L $JDK/jre/lib/i386
-L $JDK/jre/lib/i386/client
-ljvm -lpthread
-o GUIdooey


Please help me translate that command to speak Mac (OS 10.4):
This is the line I have been using up to now:
gcc -framework JavaVM GUIdooey.c -o guidooey
It works for all my JNI stuff until there is an attempt to create a JFrame (either
directly, like the following code) or indirectly (from an already- launched Java
method).


#include "/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/ Headers/jni.h"
#include <string.h>
#include <stdlib.h>


void bomb(int errno, char * msg) {
	printf("ERROR: %s\n", msg);
	exit(errno);
	}

int main(int argc, char *args[]) {
	jclass		jframe_cls;
	JNIEnv		*env;
	JavaVM		*jvm;
	JavaVMOption options[10];
	JavaVMInitArgs vm_args;
	jmethodID mid;
	jobject jframe;
	
	options[0].optionString= "-Djava.class.path=.";
	memset(&vm_args, 0, sizeof(vm_args));
	vm_args.version = JNI_VERSION_1_4;
	vm_args.nOptions = 1;
	vm_args.options = options;
	vm_args.ignoreUnrecognized= 0;
	long status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
	if (status==JNI_ERR) bomb(1000, "cannot create JVM");

jframe_cls = (*env)->FindClass(env, "javax/swing/JFrame");
if (jframe_cls==0) bomb(1003, "cannot find JFrame class.");

mid = (*env)->GetMethodID(env,jframe_cls,"<init>","(Ljava/lang/ String;)V");
if (mid==0) bomb(1004, "couldn't find constructor");
printf("found constructor\n");

jstring title= (*env)->NewStringUTF(env, "JFrame from C!");
if (title==NULL) bomb(1005,"JNI failed to get memory for string");
jframe = (*env)->NewObject(env,jframe_cls,mid,title);
printf("jframe created\n");

return 0;
}


The result is that the "found constructor" message AND the "jframe created"
message both appear, but in BETWEEN them is a long exception traceback.
sj


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.