java调用C程序
案例
如下:
package com.laction.so
linux:/mnt/zddom/com/laction/so
-rw-r--r--. 1 root root 922 nov 13 14:30 hellojni.class
-rw-r--r--. 1 root root 463 nov 13 14:29 hellojni.java
hellojni.java
package com.laction.so;
public class hellojni {
public native void printhello();
public native void printstring(string str);
public native string printstringretrun(string str);
static {
system.loadlibrary("hellojni");
}
public static void main(string args[]) {
hellojni myjni = new hellojni();
myjni.printhello();
myjni.printstring("hello world from printstring fun");
system.out.println("java:" + myjni.printstringretrun("test"));
}
}
//操作要放在mnt/zddom中
javac com/laction/so/hellojni.java
会在同一个目录中生成class文件
接下来生成.h文件
com_laction_so_hellojni.h
/* do not edit this file - it is machine generated */
#include <jni.h>
/* header for class com_laction_so_hellojni */
#ifndef _included_com_laction_so_hellojni
#define _included_com_laction_so_hellojni
#ifdef __cplusplus
extern "c" {
#endif
/*
* class: com_laction_so_hellojni
* method: printhello
* signature: ()v
*/
jniexport void jnicall java_com_laction_so_hellojni_printhello
(jnienv *, jobject);
/*
* class: com_laction_so_hellojni
* method: printstring
* signature: (ljava/lang/string;)v
*/
jniexport void jnicall java_com_laction_so_hellojni_printstring
(jnienv *, jobject, jstring);
/*
* class: com_laction_ljw_so_hellojni
* method: printstringretrun
* signature: (ljava/lang/string;)ljava/lang/string;
*/
jniexport jstring jnicall java_com_laction_so_hellojni_printstringretrun
(jnienv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
上面生成的文件需要简单的改动一下,如下:类型加参数
jniexport void jnicall java_com_laction_so_hellojni_printhello
(jnienv * env, jobject o);
完整文件参数例子
com_laction_so_hellojni.c
/* do not edit this file - it is machine generated */
#include "com_laction_so_hellojni.h"
#include <stdio.h>
/* header for class com_laction_so_hellojni */
/*
* class: com_laction_so_hellojni
* method: printhello
* signature: ()v
*/
jniexport void jnicall java_com_laction_so_hellojni_printhello(jnienv * env, jobject o)
{
return ;
}
/*
* class: com_laction_so_hellojni
* method: printstring
* signature: (ljava/lang/string;)v
*/
jniexport void jnicall java_com_laction_so_hellojni_printstring(jnienv * env, jobject o, jstring s)
{
const char *str = (*env)->getstringutfchars(env,s,0);
//printf("%s!\n", str);
return;
}
/*
* class: com_laction_so_hellojni
* method: printstringretrun
* signature: (ljava/lang/string;)ljava/lang/string;
*/
jniexport jstring jnicall java_com_laction_so_hellojni_printstringretrun(jnienv * env, jobject o, jstring s)
{
return s;
}
//操作要放在mnt/zddom中
gcc -fpic -i $java_home/include -i $java_home/include/linux -shared -o hellojni.so com_laction_ljw_so_hellojni.c
hellojni.so
如果顺利生成so文件,则ok
如果不能,则检查路径和java.library.path
如何找java.library.path
/************如何找java.library.path***************/
public class helloworld{
public static void main(string args[]){
system.out.println("hello! world");
system.out.println(system.getproperty("java.library.path"));
}
}
/************自定义一个文件夹***************/
常见问题:no hellonative in java.library.path
如:
/mnt/so
修改/etc/profile
vim /etc/profile
/////
export java_home=/usr/local/java/jdk1.8.0_171
export maven_home=/usr/local/maven/maven3
export classpath=.:$java_home/lib/dt.jar:$java_home/lib/tools.jar
export path=$path:$java_home/bin:$maven_home/bin
加入
ld_library_path=/mnt/so
export ld_library_path
上一篇: Core Animation-1:图层树
下一篇: 幽默讽刺,讽的真好笑