Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Feb 2002 15:15:56 -0500 (EST)
From:      Mikhail Kruk <meshko@cs.brandeis.edu>
To:        Bill Huey <billh@gnuppy.monkey.org>
Cc:        Guerry Semones <gsemones@treenleaf.com>, <freebsd-java@FreeBSD.ORG>
Subject:   Re: Need info for compiling JDK1.3.1 on FreeBSD with Native Threads
Message-ID:  <Pine.LNX.4.44.0202141509090.13821-100000@daedalus.cs.brandeis.edu>
In-Reply-To: <20020214194925.GA1818@gnuppy.monkey.org>

next in thread | previous in thread | raw e-mail | index | archive | help
> Uh, you can't really call the normal IO functions, read()/write(), etc...
> without it piping through the thread system in some way. If it's green threads,
> then any function like that the coexistent program uses must go through
> that layer of thread-managed wrapped functions and not the normal functions
> in libc, etc... I'm not sure of the linking conventions off hand so I don't
> know how to export those wrapped functions to a companion program.

I don't know much, sorry. I know that you can create JVM, it will run in 
its own thread and it will be possible to attach your native C thread to 
it. Here is the tutorial
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/jniref.html

and here is an example of how it is done:

#include <jni.h>
#include <pthread.h>

JavaVM *jvm;

void *native_thread(void *arg) {
  JNIEnv *env;
  jclass cls;
  jmethodID mid;
  jfieldID fid;
  jint result;
  jobject jobj;
  JavaVMAttachArgs args;
  jint asize;
  
  args.version= JNI_VERSION_1_2;
  args.name="user";
  args.group=NULL;
  result=(*jvm)->AttachCurrentThread(
	jvm, (void **)&env, &args);

  cls = (*env)->FindClass(env,"ArrayHandler");
  if( cls == NULL ) {
    printf("can't find class ArrayHandler\n");
    exit (-1);
  }
  (*env)->ExceptionClear(env);
  mid=(*env)->GetMethodID(env, cls, "<init>", "()V");
  jobj=(*env)->NewObject(env, cls, mid);
  fid=(*env)->GetFieldID(env, cls, "arraySize", "I");
  asize=(*env)->GetIntField(env, jobj, fid);
  printf("size of array is %d\n",asize);
  (*jvm)->DetachCurrentThread(jvm);
}

void main(int argc, char *argv[], char **envp) {
  JavaVMOption *options;
  JavaVMInitArgs vm_args;
  JNIEnv *env;
  jint result;
  pthread_t tid;
  int thr_id;
  int i;

  options = (void *)malloc(3 * sizeof(JavaVMOption));

  options[0].optionString = "-Djava.class.path=.";
  options[1].optionString = "-Djava.compiler=NONE";

  vm_args.version = JNI_VERSION_1_2;
  vm_args.options = options;
 vm_args.nOptions = 2;
  vm_args.ignoreUnrecognized = JNI_FALSE;

  result = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args);
  if(result == JNI_ERR ) {
    printf("Error invoking the JVM");
    exit (-1);
  }

  thr_id=pthread_create(&tid, NULL, native_thread, NULL);

// If you don't have join, sleep instead
//sleep(1000);
  pthread_join(tid, NULL);
  (*jvm)->DestroyJavaVM(jvm);
  exit(0);
}






To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-java" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.LNX.4.44.0202141509090.13821-100000>