Java 本机方法链接错误
我第一次尝试本地方法……Java 本机方法链接错误,java,dll,native-methods,Java,Dll,Native Methods,我第一次尝试本地方法…… 我从这个链接中获取了一个简单的编程 nativetest.java public class nativetest { static { System.loadLibrary("nativetest"); } public native String sayHello(String s);
我从这个链接中获取了一个简单的编程
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.javajavah-jni nativetest
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);
}
}
已成功创建nativetest.h文件
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);
}
}
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
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
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);
}
}
已成功创建共享对象文件
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);
}
}
当我执行nativetest时,它显示以下错误
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);
}
}
java-Djava.library.path=。本地测试线程“main”java.lang.UnsatifiedLinkError中出现异常:java.library.path中没有nativetest 位于java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
在java.lang.Runtime.loadLibrary0(Runtime.java:845)
在java.lang.System.loadLibrary(System.java:1084)上
在nativetest(nativetest.java:4)
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);
}
}
在Linux上,共享库的文件名应该以“lib”开头,即“lib[library\u name].so”
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);
}
}
参考:
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);
}
}
每个共享库都有一个名为“soname”的特殊名称。soname的前缀为“lib”、库的名称、短语为“`.so”“,后跟句点和版本号,每当接口更改时,版本号都会递增(作为特殊例外,最低级别的C库不会以“lib”开头)。一个完全限定的soname包含它所在的目录作为前缀;在工作系统上,完全限定的soname只是指向共享库“真实名称”的符号链接。
将
添加到LD_LIBRARY_PATH
?尝试将nativetest.so
重命名为libnativetest.so
@lhuang Thanx它正在工作
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);
}
}