JNI詳細001_c++

1405 ワード

public class HelloWorld {
   public native void print(String str);  //      
   
   public static void main(String[] args) {
	   new HelloWorld().print("world!");
	   //System.out.println(System.getProperty("java.library.path"));
   }
   
   static{
	   //System.loadLibrary("helloworld"); //      
	   System.load("/Users/zhaoshun/solibs/libhello.so");
   }
}

/**
 *  
 * Class:     jni_sample_HelloWorld
 * Method:    print
 * Signature: (Ljava/lang/String;)V
 *
  JNIEXPORT void JNICALL Java_jni_sample_HelloWorld_print(JNIEnv *, jobject, jstring);
 */
#include <iostream.h>
#include <jni.h>
#include "jni_sample_HelloWorld.h"
using namespace std;

JNIEXPORT void JNICALL Java_jni_sample_HelloWorld_print(JNIEnv *env, jobject obj, jstring name)
{
	const char* pname = env->GetStringUTFChars(name,NULL);
	cout <<"Hello,"<< pname << endl;
}

/*
c        
str = (*env)->GetStringUTFChars(env,prompt,NULL);  
*/

コンパイル
g++ -dynamiclib -o libhello.so HelloWorld.cpp -framework JavaVM -I/System/Library/Frameworks/JavaVM.framework/Headers
実行結果:
Hello,world!