javac HelloWorld.java
ld -G HelloWorld.o -o libHelloWorld.so
java -Djava.library.path=. HelloWorld
Hello World!
setenv LD_LIBRARY_PATH .
来代替
)当前目录
”.”作为Java装载库的缺省路径。
JNIEXPORT void JNICALL
Java_ ... (JNIENV *env, ...) {
(*env)->GetFieldID (env, ...);
}
JNIEXPORT void JNICALL
Java_ ... (JNIENV *env, ...) {
env->GetFieldID (...);
}
jclass c = (*env)->GetObjectClass(env, this);
jfieldID fid = (*env)->GetFieldID(env, c, "private_variable", "I");
(*env)->SetIntField (env, this, fid, val);
jclass c = (*env)->GetObjectClass(env, this);
jfieldID fid = (*env)->GetFieldID(env, c, "private_variable", "I");
jint val = (*env)->GetIntField(env, this, fid);
jclass c = (*env)->GetObjectClass(env, this);
JNIEXPORT void JNICALL
Java_AccessOther_setPrivate (JNIENV *env, jobject this, jobject other, jint val) {
jclass c = (*env)->GetObjectClass(env, other);
}
private String getLine (String) (Ljava/lang/String;)Ljava/lang/String;
public static void main (String [] args) ([Ljava/lang/String;)V
JNI type CallStatictypeMethod (jobject, jmethodID, arguments);
JNI type CallNonvirtualtypeMethod (jobject, jmethodID, arguments);
JNItypeCalltypeMethodA (jobject obj, jmethodID mid, jvalue* args);
JNItypeCalltypeMethodV (jobject obj, jmethodID mid, va_list args);
NewObjectArray
GetArrayLength
SetObjectElement
JNIEXPORT jstring JNICALL
Java_Strings_printString (JNIEnv *env, jclass this, jstring str) {
const char* utf_string;
jboolean is_copy;
utf_string = env->GetStringUTFChars (str, &isCopy);
printf ("%s\n", utf_string);
if (isCopy == JNI_TRUE) {
env->ReleaseStringUTFChars (str, utf_string);
}
}
JNIEXPORT jstring JNICALL
Java_NewString_newStringUTF (JNIEnv *env, jclass this) {
const char* msg = "String constructed using NewStringUTF";
return env->NewStringUTF(msg);
}
jclass cls = (*env)->FindClass (env, "java/lang/IllegalArgumentException");
if (cls==NULL) return; // Give up
(*env)->ThrowNew (env, cls, "message");
return;
(*env)->CalltypeMethod (env, obj, method_id)
jthrowable exc = (*env)->ExceptionOccurred(env)
if (exc) {
(*env)->ExceptionClear(env)
}
int main (int argc, char *argv[]) {
JNIEnv *env;
JavaVM *jvm;
int i;
JDK1_1InitArgs vm_args; // env variables to control invocation
jint ret;
jmethodID mid;
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
int len = strlen(MY_CLASSPATH) + strlen(vm_args.classpath) + 1;
char * newcp = (char*) malloc(len * sizeof(char));
strcpy(newcp, MY_CLASSPATH);
strcat(newcp, vm_args.classpath);
vm_args.classpath = newcp;
vm_args.properties = properties;
vm_args.vfprintf = y_fprintf;
vm_args.exit = y_exit;
vm_args.abort = y_abort;
ret = JNI_CreateJavaVM(&jvm,&env,&vm_args);
if (ret < 0) {
fprintf(stderr, "Can't create Java VM. Error: %ld\n", ret);
return(1);
}
if (argc < 2) {
printf("No class specified. Exiting...\n\n");
return(-1);
}
jstring jstr = env->NewStringUTF("");
jobjectArray str_array =
env->NewObjectArray(argc-2, env->FindClass("java/lang/String"), jstr);
// Pass main arguments to Java
for( i = 2; i < argc; i++ ) {
jstr = env->NewStringUTF (argv[i]);
if (jstr == 0) {
fprintf(stderr, "Out of memory\n");
return(1);
}
env->SetObjectArrayElement( str_array, i - 2, jstr);
}
jclass clazz = env->FindClass(dotsToSlashes(argv[1]));
if (clazz == 0) {
fprintf(stderr, "Can't locate the %s class. Exiting...\n", argv[1]);
return(1);
}
mid = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
if (mid == 0) {
fprintf(stderr, "Can't locate the main method. Exiting...\n");
return(1);
}
env->CallStaticVoidMethod (clazz, mid, str_array);
jvm->DestroyJavaVM();
return(0);
}