[转]Ubuntu下使用Jni开发例子
2016-10-23 22:23
246 查看
http://www.cnblogs.com/zxglive2006/archive/2012/01/15/2323110.html
先用eclipse 创建 Java Project;
然后直接在项目中添加Prompt.java文件,放在default package下(最好不要添加包,否则容易出错)。
1. 编写Java文件,在其中声明native方法, 并通过static 语句块加载动态链接库,示例Prompt.java代码如下:
class Prompt {
private native String getLine(String prompt);
public static void main(String args[]) {
Prompt p = new Prompt();
String input = p.getLine("Type a line: ");
System.out.println("User typed: " + input);
}
static {
System.loadLibrary("Prompt"); //这里到so库名千万别搞错了, Prompt对应的实际库名称是libPrompt.so
}
}
2.调用javac命令生成Prompt.class文件;
javac Prompt.java
3.调用javah命令生成Prompt.h头文件供C程序引用:
javah -jni Prompt
自动生成的头文件如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Prompt */
#ifndef _Included_Prompt
#define _Included_Prompt
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Prompt
* Method: getLine
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_Prompt_getLine
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
4.编写Prompt.c文件实现具体功能:
#include <jni.h>
#include <stdio.h>
#include "Prompt.h"
JNIEXPORT void JNICALL
Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)
{
char buf[128];
const jbyte *str;
str = (*env)->GetStringUTFChars(env, prompt, NULL);
if(str == NULL) {
return NULL;
}
printf("%s", str);
(*env)->ReleaseStringUTFChars(env, prompt, str);
scanf("%s", buf);
return (*env)->NewStringUTF(env, buf);
}
5. 编译动态库libPrompt.so;
可能会报如下错误:
/usr/bin/ld: /tmp/ccG1IYKj.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/tmp/ccG1IYKj.o: error adding symbols: 错误的值
collect2: error: ld returned 1 exit status
加上编译选项-fPIC即可;
gcc -shared -fpic -I/usr/lib/jvm/java-6-sun-1.6.0.26/include -I/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux Prompt.c -o libPrompt.so
(gcc -shared -fPIC -I /opt/Java/jdk1.8.0_71/include/ -I /opt/Java/jdk1.8.0_71/include/linux/ Prompt.c -o libPrompt.so)
6. 运行。
java Prompt
可能会报如下错误
Exception in thread "main" java.lang.UnsatisfiedLinkError: no test in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at Test.<clinit>(Test.java:14)
需要设置java.library.path的路径来执行:
java -Djava.library.path=. Prompt
先用eclipse 创建 Java Project;
然后直接在项目中添加Prompt.java文件,放在default package下(最好不要添加包,否则容易出错)。
1. 编写Java文件,在其中声明native方法, 并通过static 语句块加载动态链接库,示例Prompt.java代码如下:
class Prompt {
private native String getLine(String prompt);
public static void main(String args[]) {
Prompt p = new Prompt();
String input = p.getLine("Type a line: ");
System.out.println("User typed: " + input);
}
static {
System.loadLibrary("Prompt"); //这里到so库名千万别搞错了, Prompt对应的实际库名称是libPrompt.so
}
}
2.调用javac命令生成Prompt.class文件;
javac Prompt.java
3.调用javah命令生成Prompt.h头文件供C程序引用:
javah -jni Prompt
自动生成的头文件如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Prompt */
#ifndef _Included_Prompt
#define _Included_Prompt
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Prompt
* Method: getLine
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_Prompt_getLine
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
4.编写Prompt.c文件实现具体功能:
#include <jni.h>
#include <stdio.h>
#include "Prompt.h"
JNIEXPORT void JNICALL
Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)
{
char buf[128];
const jbyte *str;
str = (*env)->GetStringUTFChars(env, prompt, NULL);
if(str == NULL) {
return NULL;
}
printf("%s", str);
(*env)->ReleaseStringUTFChars(env, prompt, str);
scanf("%s", buf);
return (*env)->NewStringUTF(env, buf);
}
5. 编译动态库libPrompt.so;
可能会报如下错误:
/usr/bin/ld: /tmp/ccG1IYKj.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/tmp/ccG1IYKj.o: error adding symbols: 错误的值
collect2: error: ld returned 1 exit status
加上编译选项-fPIC即可;
gcc -shared -fpic -I/usr/lib/jvm/java-6-sun-1.6.0.26/include -I/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux Prompt.c -o libPrompt.so
(gcc -shared -fPIC -I /opt/Java/jdk1.8.0_71/include/ -I /opt/Java/jdk1.8.0_71/include/linux/ Prompt.c -o libPrompt.so)
6. 运行。
java Prompt
可能会报如下错误
Exception in thread "main" java.lang.UnsatisfiedLinkError: no test in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at Test.<clinit>(Test.java:14)
需要设置java.library.path的路径来执行:
java -Djava.library.path=. Prompt
相关文章推荐
- Ubuntu下使用Jni开发例子
- Ubuntu下使用Jni开发例子
- Ubuntu下使用Jni开发例子
- Ubuntu上使用android4.0.3模拟器实现JNI例子
- Android中使用NDK环境开发JNI程序例子
- 使用eclipse开发ndk之:导入现有的ndk工程(NDK例子-Hello-jni导入到eclipse)
- ubuntu上最使用jni最简单易懂的例子
- 使用eclipse开发ndk之:导入现有的ndk工程(NDK例子-Hello-jni导入到eclipse)(转载)
- Ubuntu 使用Jni开发实例详解
- 使用eclipse开发ndk之:导入现有的ndk工程(NDK例子-Hello-jni导入到eclipse)
- Ubuntu上使用android4.0.3模拟器实现JNI例子
- 01 在 Ubuntu/CentOS 中使用 Eclipse 来开发 MapReduce 程序 小例子
- JAVA调用Tuxedo服务——使用JNI封装Tuxedo客户端的一个例子
- 如何在Ubuntu中使用Eclipse + CDT开发C/C++程序
- fcntl使用的例子 (源自 嵌入式Linux 应用程序开发详解)
- Ubuntu10.04 下使用code Blocks 进行oci 程序开发
- Ubuntu使用点滴(3)-C/C++开发环境配置
- Java使用JNI调用VC的DLL的一个小例子
- 使用servlet开发动态wap的一个简单例子
- 使用VirtualBox安装Ubuntu9.10,搭建java开发环境总结