参考资料:
http://docs.oracle.com/javase/6/docs/index.html
http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html java本地接口规范
官方提供的说明。
语言的相互调用,首先要解决数据类型之间的转换。JNI提供了Java类型映射到本地C类型的转换。
原始类型
引用类型
实例
public class DataType
{
static
{
System.loadLibrary("datatype");
}
public native byte getByte(byte b);
public native short getShort(short s);
public native int getInt(int i);
public native long getLong(long lon);
public native float getFloat(float f);
public native double getDouble(double d);
public native char getChar(char c);
public native boolean getBoolean(boolean bool);
public native void get();
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class demo_dll_DataType */
#ifndef _Included_demo_dll_DataType
#define _Included_demo_dll_DataType
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: demo_dll_DataType
* Method: getByte
* Signature: (B)B
*/
JNIEXPORT jbyte JNICALL Java_demo_dll_DataType_getByte
(JNIEnv *, jobject, jbyte b)
{
++b;
return b;
}
/*
* Class: demo_dll_DataType
* Method: getShort
* Signature: (S)S
*/
JNIEXPORT jshort JNICALL Java_demo_dll_DataType_getShort
(JNIEnv *, jobject, jshort s)
{
++s;
return s;
}
/*
* Class: demo_dll_DataType
* Method: getInt
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_demo_dll_DataType_getInt
(JNIEnv *, jobject, jint i)
{
++i;
return i;
}
/*
* Class: demo_dll_DataType
* Method: getLong
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_demo_dll_DataType_getLong
(JNIEnv *, jobject, jlong lon)
{
++lon;
return lon;
}
/*
* Class: demo_dll_DataType
* Method: getFloat
* Signature: (F)F
*/
JNIEXPORT jfloat JNICALL Java_demo_dll_DataType_getFloat
(JNIEnv *, jobject, jfloat f)
{
++f;
return f;
}
/*
* Class: demo_dll_DataType
* Method: getDouble
* Signature: (D)D
*/
JNIEXPORT jdouble JNICALL Java_demo_dll_DataType_getDouble
(JNIEnv *, jobject, jdouble d)
{
++d;
return d;
}
/*
* Class: demo_dll_DataType
* Method: getChar
* Signature: (C)C
*/
JNIEXPORT jchar JNICALL Java_demo_dll_DataType_getChar
(JNIEnv *, jobject, jchar c)
{
return c;
}
/*
* Class: demo_dll_DataType
* Method: getBoolean
* Signature: (Z)Z
*/
JNIEXPORT jboolean JNICALL Java_demo_dll_DataType_getBoolean
(JNIEnv *, jobject, jboolean boo)
{
return !boo;
}
/*
* Class: demo_dll_DataType
* Method: get
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_demo_dll_DataType_get
(JNIEnv *, jobject)
{
printf("hello world!\n");
}
#ifdef __cplusplus
}
#endif
#endif
测试:
public class Test
{
public static void main(String[] args)
{
DataType dt = new DataType();
dt.get();
System.out.println(dt.getBoolean(true));
System.out.println(dt.getByte((byte)1));
System.out.println(dt.getChar('c'));
System.out.println(dt.getDouble(3.1234));
System.out.println(dt.getFloat(2.35F));
System.out.println(dt.getInt(123));
System.out.println(dt.getLong(456));
System.out.println(dt.getShort((short) 45));
}
}
结果:
false
2
c
4.1234
3.35
124
457
46
hello world!
http://docs.oracle.com/javase/6/docs/index.html
http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html java本地接口规范
官方提供的说明。
语言的相互调用,首先要解决数据类型之间的转换。JNI提供了Java类型映射到本地C类型的转换。
原始类型
引用类型
实例
public class DataType
{
static
{
System.loadLibrary("datatype");
}
public native byte getByte(byte b);
public native short getShort(short s);
public native int getInt(int i);
public native long getLong(long lon);
public native float getFloat(float f);
public native double getDouble(double d);
public native char getChar(char c);
public native boolean getBoolean(boolean bool);
public native void get();
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class demo_dll_DataType */
#ifndef _Included_demo_dll_DataType
#define _Included_demo_dll_DataType
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: demo_dll_DataType
* Method: getByte
* Signature: (B)B
*/
JNIEXPORT jbyte JNICALL Java_demo_dll_DataType_getByte
(JNIEnv *, jobject, jbyte b)
{
++b;
return b;
}
/*
* Class: demo_dll_DataType
* Method: getShort
* Signature: (S)S
*/
JNIEXPORT jshort JNICALL Java_demo_dll_DataType_getShort
(JNIEnv *, jobject, jshort s)
{
++s;
return s;
}
/*
* Class: demo_dll_DataType
* Method: getInt
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_demo_dll_DataType_getInt
(JNIEnv *, jobject, jint i)
{
++i;
return i;
}
/*
* Class: demo_dll_DataType
* Method: getLong
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_demo_dll_DataType_getLong
(JNIEnv *, jobject, jlong lon)
{
++lon;
return lon;
}
/*
* Class: demo_dll_DataType
* Method: getFloat
* Signature: (F)F
*/
JNIEXPORT jfloat JNICALL Java_demo_dll_DataType_getFloat
(JNIEnv *, jobject, jfloat f)
{
++f;
return f;
}
/*
* Class: demo_dll_DataType
* Method: getDouble
* Signature: (D)D
*/
JNIEXPORT jdouble JNICALL Java_demo_dll_DataType_getDouble
(JNIEnv *, jobject, jdouble d)
{
++d;
return d;
}
/*
* Class: demo_dll_DataType
* Method: getChar
* Signature: (C)C
*/
JNIEXPORT jchar JNICALL Java_demo_dll_DataType_getChar
(JNIEnv *, jobject, jchar c)
{
return c;
}
/*
* Class: demo_dll_DataType
* Method: getBoolean
* Signature: (Z)Z
*/
JNIEXPORT jboolean JNICALL Java_demo_dll_DataType_getBoolean
(JNIEnv *, jobject, jboolean boo)
{
return !boo;
}
/*
* Class: demo_dll_DataType
* Method: get
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_demo_dll_DataType_get
(JNIEnv *, jobject)
{
printf("hello world!\n");
}
#ifdef __cplusplus
}
#endif
#endif
测试:
public class Test
{
public static void main(String[] args)
{
DataType dt = new DataType();
dt.get();
System.out.println(dt.getBoolean(true));
System.out.println(dt.getByte((byte)1));
System.out.println(dt.getChar('c'));
System.out.println(dt.getDouble(3.1234));
System.out.println(dt.getFloat(2.35F));
System.out.println(dt.getInt(123));
System.out.println(dt.getLong(456));
System.out.println(dt.getShort((short) 45));
}
}
结果:
false
2
c
4.1234
3.35
124
457
46
hello world!