xbfLibR.dll generated by Delphi, Error in Ubuntu:
java.lang.UnsatisfiedLinkError: /tmp/xbfLibR1872218867548961332.dll: /tmp/xbfLibR1872218867548961332.dll:
invalid ELF header (Possible cause: endianness mismatch)
- JNI Header
- C++
JNI Header
- Write a Java Class ReadXbf.java that uses C/C++ Codes
package com.cartionsoft.xbf;
public class ReadXbf {
static {
System.loadLibrary("XbfLibR1"); // Load native library XbfLibR1.dll (Windows) or libXbfLibR1.so (Unixes)
// at runtime
// This library contains a native method called sayHello()
}
// Declare an instance native method sayHello() which receives no parameter and returns void
private native String sayHello(int num, String text);
// Test Driver
public static void main(String[] args) {
ReadXbf rf = new ReadXbf();
String text = rf.sayHello(1, "world"); // Create an instance and invoke the native method
System.out.println(text);
}
}
- Generate the JNI Header File
javac -h . ReadXbf.java
JNI with C++
XbfLibR1.cpp:
#include <string.h>
#include "com_cartionsoft_xbf_ReadXbf.h" // Generated
JNIEXPORT jstring
JNICALL
Java_com_cartionsoft_xbf_ReadXbf_sayHello(
JNIEnv *env, jobject obj,
jint num,
jstring string) {
jstring result;
char msg[60];
const char *name = env->GetStringUTFChars(string, NULL);//Java String to C Style string
if (num < 5)
strcpy(msg, "Hello ");
else
strcpy(msg, "NiHao ");
strcat(msg, name);
env->ReleaseStringUTFChars(string, name);
result = env->NewStringUTF(msg); // C style string to Java String
return result;
}
Generate .so
- linux
g++ -shared -fPIC -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" XbfLibR1.cpp -o libXbfLibR1.so
!!!Note!!!:
System.loadLibrary()
is the file name
that requires a fixed format
By example, libXXXX.so for linux family, your XbfLibR1.so should rename to libXbfLibR1.so
launch
- exec
java com.cartionsoft.xbf.ReadXbf
- temporary exec
java -Djava.library.path=. com.cartionsoft.xbf.ReadXbf
java library
vi ~/.bashrc
add lib path:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/xb
Effective immediately:
source ~/.bashrc
- see library
java -XshowSettings:properties
info:
java.library.path =
/home/xb
/usr/java/packages/lib/amd64
/usr/lib/x86_64-linux-gnu/jni
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/lib/jni
/lib
/usr/lib