jni小例子

xiaoxiao2026-08-30  20

根据网上的做法,老出现jni java.lang.unsatisfie.lang.unsatisfiedlinkerror ,问题答案

http://dikar.iteye.com/blog/382701

   1.先写个java类,写个本地方法 编译后 javah Test生成 Test.h

public class Test{ static{ System.loadLibrary("hello"); } public native void hello(); public static void main(String args[]){ new Test().hello(); } }

  

#include <stdio.h> #include <stdlib.h> #include "Test.h" JNIEXPORT void JNICALL _Java_Test_hello(JNIEnv * a, jobject b){ //一样注意_java 有个斜杠 printf("helloWorld"); }

 

/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class Test */ #ifndef _Included_Test #define _Included_Test #ifdef __cplusplus extern "C" { #endif /* * Class: Test * Method: hello * Signature: ()V */ JNIEXPORT void JNICALL _Java_Test_hello (JNIEnv *, jobject); //_Java_Test_hello 注意,java前边有个杠,是手动加上去的,因为java.lang.unsatisfie.lang.unsatisfiedlinkerror #ifdef __cplusplus } #endif #endif

  gcc命令编译成hello.dll ,我用的MInGw 命令都是网上的

  gcc -shared -Wl, -o hello.dll -ID:\Sun\SDK\jdk\include -ID:\Sun\SDK\jdk\include\win32 Test.c

 

然后java Test运行,控制台输出 helloWorld

 

 

返回String

JNIEXPORT jstring JNICALL _Java_Test_getString(JNIEnv *a, jobject o){ //printf("helloWorld"); jstring jstr; char* sa[]={"hello","world"}; jstr=(*a)->NewStringUTF(a,sa[0]); return jstr;}

相关资源:android JNI简单例子
转载请注明原文地址: https://www.6miu.com/read-5052088.html

最新回复(0)