前言
jni部分系列文章
- <a href="http://www.reibang.com/p/476aae319808"> jni基本數(shù)據(jù)類型的傳遞</a>
- <a href="http://www.reibang.com/p/e397382ba810"> jni 中較為復(fù)雜的數(shù)據(jù)類型(String和數(shù)組)</a>
這篇博客承接上一篇,是系列中的第三篇碳柱,本文主要描述jni中java類對象的傳遞和操作也祠,包括操作對象和新建對象兩種操作
操作對象
1.建立一個簡單的java類,用于操作
package model;
/**
* Created by act64 on 2017/5/29.
*/
public class Screen {
int height;
int width;
public Screen(int width,int height){
this.height=height;
this.width=width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public void printInfo(){
System.out.println(" screen height = "+ height+" width = "+width);
}
}
2.創(chuàng)建java的jni和主程序入口
import model.Screen;
/**
* Created by act64 on 2017/5/29.
*/
public class JniClassHello {
static {
System.loadLibrary("helloClass");
}
public static void main(String[] args){
Screen s=new Screen(1440,720);
s.printInfo();
Screen screenBigger = changeScreen(s);
screenBigger.printInfo();
if (screenBigger.equals(s)){
System.out.println("屏幕是同一塊");
}
}
public native static Screen changeScreen(Screen screen);
}
這兩部分都是十分簡單的有巧,就不加以描述了
3.建立JNI c文件
文件名 hello.c
#include <stdio.h>
#include <jni.h>
#include <stdlib.h>
//jni映射java的方法鹉胖,可以通過javah命令生成,需要的看系列文章1和2
jobject c_hello(JNIEnv * env, jobject mJobject,jobject jScreenObj){
//獲得java的 class對象绒尊,用于找到方法和構(gòu)造函數(shù)等
//對于java反射有所了解的話畜挥,操作起來會習(xí)慣點
jclass screenCls = (*env)->GetObjectClass(env, jScreenObj);
//獲得函數(shù)“setHight ” ,描述符是(I)V婴谱,即參數(shù)為int 返回void
//描述符都可以用javah生成頭文件然后復(fù)制過來蟹但,很方便
jmethodID setHight =
(*env)->GetMethodID(env, screenCls, "setHeight", "(I)V");
if (setHight == NULL) {
return; /* method not found */
}
printf("ChangeScreenHeightTo 800\n");
//調(diào)用一個返回值為void的java方法,參數(shù)是va_list不定參數(shù)
(*env)->CallVoidMethod(env, jScreenObj, setHight,800);
return jScreenObj;
}
// 建立jni映射表谭羔,將c和java的函數(shù)關(guān)聯(lián)起來
const JNINativeMethod methods[]={
{"changeScreen","(Lmodel/Screen;)Lmodel/Screen;",(jobject *)c_hello},
};
//jni load時的初始化函數(shù)回調(diào)
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
JNIEnv *env;
jclass cls;
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
return JNI_ERR; /* JNI version not supported */
}
cls = (*env)->FindClass(env, "JniClassHello");
if (cls == NULL) {
return JNI_ERR;
}
//注冊映射表
if((*env)->RegisterNatives(env,cls,methods,1)<0){
return JNI_ERR;
}
return JNI_VERSION_1_4;
}
4.編譯和運行
javac JniClassHello.java
gcc -fPIC -I /usr/lib/jvm/java-1.7.0-openjdk-amd64/include -shared -o libhelloClass.so hello.c
export LD_LIBRARY_PATH=./
java JniClassHello
運行結(jié)果如下
新建對象
新建對象的用法和操作對象很像华糖,只不過操作的是構(gòu)造函數(shù)
1.先改寫 java文件,加入create1080Screen Native方法,并調(diào)用
import model.Screen;
/**
* Created by act64 on 2017/5/29.
*/
public class JniClassHello {
static {
System.loadLibrary("helloClass");
}
public static void main(String[] args){
Screen s=new Screen(1440,720);
s.printInfo();
Screen screenBigger = changeScreen(s);
screenBigger.printInfo();
if (screenBigger.equals(s)){
System.out.println("屏幕是同一塊");
}
create1080Screen().printInfo();
}
public native static Screen changeScreen(Screen screen);
public native static Screen create1080Screen();
}
2.修改hello.c程序,加入對于create1080Screen的支持
#include <stdio.h>
#include <jni.h>
#include <stdlib.h>
jobject c_hello(JNIEnv * env, jobject mJobject,jobject jScreenObj){
jclass screenCls = (*env)->GetObjectClass(env, jScreenObj);
jmethodID setHight =
(*env)->GetMethodID(env, screenCls, "setHeight", "(I)V");
if (setHight == NULL) {
return; /* method not found */
}
printf("ChangeScreenHeightTo 800\n");
(*env)->CallVoidMethod(env, jScreenObj, setHight,800);
return jScreenObj;
}
jobject c_hello_create(JNIEnv * env, jobject mJobject){
//通過名稱查找類
jclass screenCls = (*env)->FindClass(env, "model/Screen");
if (screenCls == NULL)
{
return ;
}
//查找引用 Screen類的構(gòu)造函數(shù)瘟裸,參數(shù)為兩個int
jmethodID cid = (*env)->GetMethodID(env, screenCls,"<init>", "(II)V");
if (cid == NULL)
{
return ;
}
// 調(diào)用java 構(gòu)造函數(shù)
//類似 new Screen(1920,1080)
jobject jScreenObj=(*env)->NewObject(env, screenCls, cid, 1920,1080);
printf("Create Screen 1920*1080 \n");
//free memory
(*env)->DeleteLocalRef(env, screenCls);
return jScreenObj;
}
//擴充jni的函數(shù)映射
//RegisterNatives的參數(shù)也要變
const JNINativeMethod methods[]={
{"changeScreen","(Lmodel/Screen;)Lmodel/Screen;",(jobject *)c_hello},
{"create1080Screen","()Lmodel/Screen;",(jobject *)c_hello_create},
};
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
JNIEnv *env;
jclass cls;
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
return JNI_ERR; /* JNI version not supported */
}
cls = (*env)->FindClass(env, "JniClassHello");
if (cls == NULL) {
return JNI_ERR;
}
//加載的數(shù)量改為2
if((*env)->RegisterNatives(env,cls,methods,2)<0){
return JNI_ERR;
}
return JNI_VERSION_1_4;
}
3.編譯運行
javac JniClassHello.java
gcc -fPIC -I /usr/lib/jvm/java-1.7.0-openjdk-amd64/include -shared -o libhelloClass.so hello.c
export LD_LIBRARY_PATH=./
java JniClassHello
結(jié)果如下
小結(jié)
系列三篇博客比較完整的敘述jni的入門部分客叉,仔細閱讀很快就能上手入門jni,剩下的技能提高只能在查詢官方文檔和實際項目中提升了话告,大家共同學(xué)習(xí)兼搏,共同進步_