开发者

Is this error caused by a 64-bit library being accessed by a Java program running in a 32-bit JVM?

开发者 https://www.devze.com 2022-12-24 18:27 出处:网络
I\'m trying to create a simple Java app that uses JNI to call some native functions. I\'ve followed the examples in the JNI Programming Guide and can\'t seem to get them to work. I have the following

I'm trying to create a simple Java app that uses JNI to call some native functions. I've followed the examples in the JNI Programming Guide and can't seem to get them to work. I have the following Hello World program, written in Java:

class HelloWorld {
    private native void print();

    public static void main(String [] args) {
        new HelloWorld().print();
    }

    static {
        System.load("/home/mike/Desktop/libHelloWorld.so");
    }
}

I compile it using javac HelloWorld.java, just like normal.

I also have the C implementation of the print function, in the HelloWorld.c file:

#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"

JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj) {
    printf("Hello world!\n");
    return;
}

Then I run javah -jni HelloWorld, and finally the following:

gcc34 -shared -fpic -o libHelloWorld.so -I/<path to JDK>/include -I/<path to JDK>/include/linux HelloWorld.c

gcc34 is the name of the GCC program on my machine here at work (I don't control that) and I obviously place the real path to the JDK in that command. When I run my program, using the standard java HelloWorld, I get an error saying the following:

Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/mike/Desktop/libHelloWorld.so: /home/mike/Desktop/libHelloWorld.so: wrong ELF class: ELFCLASS64 (Possible causes: architecture word width mismatch)
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1778)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1674)
    at java.lang.Runtime.load0(Runtime.java:770)
    at java.lang.System.load(System.java:1003)
    at HelloWorld.<clinit>(HelloWorld.java:8)
Could not find the main class: HelloWorld. Program will exit.

I know I'm running a 32-bit JVM (and unfortunately, as of right now, I'm not allowed to get a 64-bit JVM). I tried telling GCC to compile in 32-bit mode using the "-m32" option, but we don't have (and again, can't get) what we need for that. EDIT: I was able to move my files to a machine able to compile in 32-bit mode. So I did that, then verified that my libHelloWorld.so file was 32-bit by runnning file libHelloWorld.so and got ELF32-bit MS开发者_StackOverflow中文版B dynamic lib SPARC Version 1, dynamically linked, not stripped, no debugging information available. I also ran java -version and got Java HotSpot(TM) Server VM (build <blah>, mixed mode) so it seems this JVM is running in 32-bit mode.

Why am I still getting this error?


Yup :-). SO won't let me submit a one-word answer, so here are some possibly-useful Google hits.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号