Calling a Java Method from Native Code
http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html
Calling Java Methods
This section illustrates how you can call Java methods from native methods. Our example program,
Callbacks.java, invokes a native method. The native method then makes a call back to a Java method. To make things a little more interesting, the Java method again (recursively) calls the native method. This process continues until the recursion is five levels deep, at which time the Java method returns without making any more calls to the native method. To help you see this, the Java method and the native method print a sequence of tracing information.Calling a Java Method from Native Code
Let us focus on the implementation of
Callbacks_nativeMethod, implemented inCallbacks.c. This native method contains a call back to the Java methodCallbacks.callback.JNIEXPORT void JNICALL
Java_Callbacks_nativeMethod(JNIEnv *env, jobject obj, jint depth)
{
jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "(I)V");
if (mid == 0)
return;
printf("In C, depth = %d, about to enter Java\n", depth);
(*env)->CallVoidMethod(env, obj, mid, depth);
printf("In C, depth = %d, back from Java\n", depth);
}You can call an instance (non-static) method by following these three steps:
- Your native method calls
GetObjectClass. This returns the Java class object to which the Java object belongs.- Your native method then calls
GetMethodID. This performs a lookup for the Java method in a given class. The lookup is based on the name of the method as well as the method signature. If the method does not exist,GetMethodIDreturns 0. An immediate return from the native method at that point causes aNoSuchMethodErrorto be thrown in Java code.- Lastly, your native method calls
CallVoidMethod. This invokes an instance method that hasvoidreturn type. You pass the object, method ID, and the actual arguments toCallVoidMethod.Forming the Method Name and Method Signature
The JNI performs a symbolic lookup based on the method's name and type signature. This ensures that the same native method will work even after new methods have been added to the corresponding Java class.
The method name is the Java method name in UTF-8 form. Specify the method name for a constructor of a class by enclosing the word
initwithin angle brackets (this appears as "<init>").Note that the JNI uses method signatures to denote the type of Java methods. The signature
(I)V, for example, denotes a Java method that takes one argument of typeintand has return typevoid. The general form of a method signature argument is:"(argument-types)return-type"The following table summarizes the encoding for the Java type signatures:
Java VM Type Signatures
Signature Java Type Z boolean B byte C char S short I int J long F float D double L fully-qualified-class ; fully-qualified-class [ type type[] ( arg-types ) ret-type method type For example, the
Prompt.getLinemethod has the signature:(Ljava/lang/String;)Ljava/lang/String;whereas the
Callbacks.mainmethod has the signature:([Ljava/lang/String;)VArray types are indicated by a leading square bracket ([) followed by the type of the array elements.
Using
javapto Generate Method SignaturesTo eliminate the mistakes in deriving method signatures by hand, you can use the
javaptool to print out method signatures. For example, by running:javap -s -p Promptyou can obtain the following output:
Compiled from Prompt.java
class Prompt extends java.lang.Object
/* ACC_SUPER bit set */
{
private native getLine (Ljava/lang/String;)Ljava/lang/String;
public static main ([Ljava/lang/String;)V
<init> ()V
static <clinit> ()V
}The "-s" flag informs
javapto output signatures rather than normal Java types. The "-p" flag causes private members to be included.Calling Java Methods Using Method IDs
In JNI, you pass the method ID to the actual method invocation function. This makes it possible to first obtain the method ID, which is a relatively expensive operation, and then use the method ID many times at later points to invoke the same method.
It is important to keep in mind that a method ID is valid only as long as the class from which it is derived is not unloaded. Once the class is unloaded, the method ID becomes invalid. So if you want to cache the method ID, make sure to keep a live reference to the Java class from which the method ID is derived. As long as the reference to the Java class (the
jclassvalue) exists, the native code keeps a live reference to the class. The section Local and Global References explains how to keep a live reference even after the native method returns and thejclassvalue goes out of scope.Passing Arguments to Java Methods
The JNI provides several ways to pass arguments to a Java method. Most often, you pass the arguments following the method ID. There are also two variations of method invocation functions that take arguments in an alternative format. For example, the
CallVoidMethodVfunction receives the arguments in ava_list, and theCallVoidMethodAfunction expects the arguments in an array ofjvalueunion types:typedef union jvalue {
jboolean z;
jbyte b;
jchar c;
jshort s;
jint i;
jlong j;
jfloat f;
jdouble d;
jobject l;
} jvalue;Besides
CallVoidMethodfunction, the JNI also supports instance method invocation functions with other return types, such asCallBooleanMethod,CallIntMethod, and so on. The return type of the method invocation function must match with the Java method you wish to invoke.Calling Static Methods
You can call static Java method from your native code by following these steps:
- Obtain the method ID using
GetStaticMethodID, as opposed toGetMethodID.- Pass the class, method ID, and arguments to the family of static method invocation functions:
CallStaticVoidMethod,CallStaticBooleanMethod, and so on.If you compare instance method invocation functions to static method invocation functions, you will notice that instance method invocation functions receive the object, rather than the class, as the second argument following the JNIEnv argument. For example, if we add a static method
static int incDepth(int depth) {return depth + 1};into
Callback.java, we can call it fromJava_Callback_nativeMethodusing:JNIEXPORT void JNICALL
Java_Callbacks_nativeMethod(JNIEnv *env, jobject obj, jint depth)
{
jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "incDepth", "(I)I");
if (mid == 0)
return;
depth = (*env)->CallStaticIntMethod(env, cls, mid, depth);Calling Instance Methods of a Superclass
You can call instance methods defined in a superclass that have been overridden in the class to which the object belongs. The JNI provides a set of
CallNonvirtual<type>Methodfunctions for this purpose. To call instance methods from the superclass that defined them, you do the following:
- Obtain the method ID from the superclass using
GetMethodID, as opposed toGetStaticMethodID).- Pass the object, superclass, method Id, and arguments to the family of nonvirtual invocation functions:
CallNonvirtualVoidMethod,CallNonvirtualBooleanMethod, and so on.It is rare that you will need to invoke the instance methods of a superclass. This facility is similar to calling a superclass method, say
f, using:super.f();in Java.
Calling a Java Method from Native Code的更多相关文章
- NDK(3)java.lang.UnsatisfiedLinkError: Native method not found解决方法
调用native方法时报错如下 : “java.lang.UnsatisfiedLinkError: Native method not found.... ”: 原因分析: 链接器只看到了在so中 ...
- java.lang.UnsatisfiedLinkError: Native method not found 三种可能解决方案
so文件编译生成后,运行时,有时候会遇到java.lang.UnsatisfiedLinkError: Native method not found问题,有可能是以下三种因素: 一.Jni方法头部大 ...
- 修改Android 4.2.2的原生Camera引出的java.lang.UnsatisfiedLinkError: Native method not found,及解决方法
修改Android 4.2.2的原生Camera应用,做一些定制,将Camera的包名从之前的 package com.android.* 修改成了com.zhao3546.*. 调整后,应用可以正常 ...
- [Xamarin] 透過Native Code呼叫 JavaScript function (转帖)
今天我們來聊聊關於如何使用WebView 中的Javascript 來呼叫 Native Code 的部分 首先,你得先來看看這篇[Xamarin] 使用Webview 來做APP因為這篇文章至少講解 ...
- NDK开发历程(一):android native code的调试方法
引用:http://www.cnblogs.com/ychellboy/archive/2013/02/22/2922683.html 使用NDK在android上做开发是一件“痛并快乐着”的差事,之 ...
- java基础-关键字-native
一. 什么是Native Method 简单地讲,一个Native Method就是一个java调用非java代码的接口.一个Native Method是这样一个java的方法:该方法的实现由 ...
- java中的native方法和修饰符(转)
Java中的native修饰符 今天偶然看代码,发现别人有这样写的方法,并且jar里面有几个dll文件,比较奇怪,于是把代码打开,发现如下写法. public native String GSMMod ...
- Java中的native方法
博客引用地址:Java中的native方法 今天花了两个小时把一份关于什么是Native Method的英文文章好好了读了一遍,以下是我依据原文的理解. 一. 什么是Native Method 简单地 ...
- Java中的native关键字与JNI
一.先说一下大致的意思: jdk提供的类库源代码中有一些方法没有实现,这些方法前有native关键字,如object类中的 : native Object clone() throws CloneNo ...
随机推荐
- NFX UNISTACK
NFX UNISTACK :http://www.nuget.org/packages/NFX/
- Java多线程(九)之ReentrantLock与Condition
一.ReentrantLock 类 1.1 什么是reentrantlock java.util.concurrent.lock 中的 Lock 框架是锁定的一个抽象,它允许把锁定的实现作为 ...
- Complex Instance Placement
转自: https://specs.openstack.org/openstack/openstack-user-stories/user-stories/proposed/complex-insta ...
- 每天一个linux命令: /etc/group文件详解
Linux /etc/group文件与/etc/passwd和/etc/shadow文件都是有关于系统管理员对用户和用户组管理时相关的文件.linux /etc/group文件是有关于系统管理员对用户 ...
- Linux+Apache+PHP+MySQL服务器环境(CentOS篇)
1.前言 CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定 ...
- u3d 场景资源打包
搞了一天终于搞定u3d的场景打包,这样就可以不用修改太多程序,把资源放在外部修改了.好处多多 但是本来很简单的东西搞了一天,google来的说作为场景scene.unity 文件 打包成 unityd ...
- idea出现插件突然失灵解决方案
File -> Settings -> Plgins 把失效的插件重新去掉打钩并重新打钩即可
- [转]Phantomjs实现获取网页快照并生成缩略图
Shell脚本实现获取网页快照并生成缩略图 这篇文章主要介绍了Shell脚本实现获取网页快照并生成缩略图,本文获取网页快照使用phantomjs.生成缩略图使用ImageMagick,需要的朋友可以参 ...
- DWZ主从表界面唯一性验证(后台验证)(三)
之前的博客介绍了前台自写js来验证主动表的唯一性,除了前台的验证,我也学习了后台的一些判断. 再次介绍一下背景需求: 利用DWZ的主从表结构批量添加课程信息,在提交表单后,触发Action事件 1.是 ...
- iview框架select默认选择一个option的值
给select加v-model,绑定的值为默认要显示的option的value值,展示的则为option的标签之间的内容,并且如果option的value是双引号,这里绑定的值也要双引号,否则不能正常 ...