Can't load AMD 64-bit .so on a AMD 64-bit platform
807575Feb 2 2009 — edited Feb 2 2009Hi,
I'm receiving the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /export/home/night64/java_c_example/libnativelib.so: Can't load AMD 64-bit .so on a AMD 64-bit platform
The following is what I have done. First I created a small java program as follows, which I called ReadFile.java
/ *** ReadFile.java ***/
import java.util.*;
class ReadFile {
//Native method declaration
native byte[] loadFile(String name);
//Load the library
static {
System.loadLibrary("nativelib");
}
public static void main(String args[]) {
byte buf[];
//Create class instance
ReadFile mappedFile=new ReadFile();
//Call native method to load ReadFile.java
buf=mappedFile.loadFile("ReadFile.java");
//Print contents of ReadFile.java
for(int i=0;i<buf.length;i++) {
System.out.print((char)buf);
}
}
}
I compiled this using javac ReadFile.java. I also generated the header file with the following command javah -jni ReadFile
I then created the following c file, nativelib.c
/*** nativelib.c ***/
#include <jni.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
extern "C" JNIEXPORT jbyteArray JNICALL Java_ReadFile_loadFile
(JNIEnv * env, jobject jobj, jstring name) {
caddr_t m;
jbyteArray jb;
std::cout << "just some simple outs" << std::endl;
jboolean iscopy;
struct stat finfo;
const char *mfile = env->GetStringUTFChars(
name, &iscopy);
int fd = open(mfile, O_RDONLY);
if (fd == -1) {
printf("Could not open %s\n", mfile);
}
lstat(mfile, &finfo);
m = mmap((caddr_t) 0, finfo.st_size,
PROT_READ, MAP_PRIVATE, fd, 0);
if (m == (caddr_t)-1) {
printf("Could not mmap %s\n", mfile);
return(0);
}
jb=env->NewByteArray(finfo.st_size);
env->SetByteArrayRegion(jb, 0,
finfo.st_size, (jbyte *)m);
close(fd);
env->ReleaseStringUTFChars(name, mfile);
return (jb);
}
This I compiled using the following command:
CC nativelib.c -c -mt -features=extensions -erroff=doubunder -xarch=generic64 -xtarget=generic64 -KPIC -xarch=generic64 -xtarget=generic64 -D_REENTRANT -DSYSV -DSVR4 -DSOLARIS -DSOLARISX86_64 -DUNIX -KPIC -DACE_HAS_EXCEPTIONS -DATH_UNICODE -I/usr/java/include -I/usr/java/include/solaris
I then did the following:
CC -xarch=generic64 -xtarget=generic64 -G -hlibnativelib.so -o libnativelib.so nativelib.o -lm -ldl
At this point I tried to run the program with java -d64 ReadFile.
I got the error as listed above. I am using java version 1.5.0_06. I am using sun c++ 5.8 compiler.
If I were to compile the above c code, and create the library using the additional flag of -library=stlport4 the program would work. Alternatively, taking the standard cout statement out of the above code, and it would also work. So it would seem to be a problem with standard c library. Unfortunately the use of stlport4 is not viable in our system, so this is not a valid solution for us.
I understand this may not be the correct forum for this post, but I am at my wits end trying to figure this out, and felt like a long shot may be worth it. Feel free to delete if this should not be here, and apologies if so.
Edited by: dirkey_wynne on Feb 2, 2009 6:35 AM