我想,第一次本地方法....
我从这个链接Click ....本地方法链接错误
nativetest.java
public class nativetest
{
static {
System.loadLibrary("nativetest");
}
public native String sayHello(String s);
public static void main(String[] argv)
{
String retval = null;
nativetest nt = new nativetest();
retval = nt.sayHello("Beavis");
System.out.println("Invocation returned " + retval);
}
}
javac nativetest.java
javah -jni nativetest
成功迈出了简单的编程nativetest.h文件已创建
nativetest.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class nativetest */
#ifndef _Included_nativetest
#define _Included_nativetest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: nativetest
* Method: sayHello
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_nativetest_sayHello(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
nativetest.c代码
nativetest.c
include "nativetest.h" /*double quotes tells it to search current directory*/
JNIEXPORT jstring JNICALL Java_nativetest_sayHello (JNIEnv *env, jobject thisobject, jstring js)
{
return js;
}
gcc -I/usr/java/jdk1.7.0_13/include -I/usr/java/jdk1.7.0_13/include/linux -o nativetest.so -shared nativetest.c
成功共享目标文件已创建。
当我执行的nativetest,它显示了以下错误
java -Djava.library.path=. nativetest
Exception in thread "main" java.lang.UnsatisfiedLinkError: no nativetest in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at nativetest.(nativetest.java:4)
Thanx提前....
加上'.'到'LD_LIBRARY_PATH'? – longhua 2013-03-20 05:34:14
尝试将'nativetest.so'重命名为'libnativetest.so'。 – longhua 2013-03-20 05:54:35
@lhuang感谢它的工作 – Rohit 2013-03-20 06:14:18