此错误是否是由在 32 位 JVM 中运行的 Java 程序访问 64 位库引起的?

发布于 2024-08-26 19:58:20 字数 2021 浏览 4 评论 0原文

我正在尝试创建一个简单的 Java 应用程序,它使用 JNI 调用一些本机函数。我已经按照 JNI 编程指南中的示例进行操作,但似乎无法让它们工作。我有以下用 Java 编写的 Hello World 程序:

class HelloWorld {
    private native void print();

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

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

我使用 javac HelloWorld.java 编译它,就像平常一样。

我还有打印函数的 C 实现,在 HelloWorld.c 文件中:

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

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

然后我运行 javah -jni HelloWorld,最后是以下内容:

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

gcc34 是我机器上的 GCC 程序的名称在工作中(我无法控制),并且我显然将 JDK 的真实路径放在该命令中。当我使用标准 java HelloWorld 运行程序时,出现以下错误:

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.

我知道我正在运行 32 位 JVM(不幸的是,截至目前,我正在运行 32 位 JVM)。不允许获得 64 位 JVM)。我尝试告诉 GCC 使用“-m32”选项以 32 位模式进行编译,但我们没有(并且再次无法获得)我们需要的东西。编辑:我能够将我的文件移动到能够在 32 位模式下编译的机器。所以我这样做了,然后通过运行 file libHelloWorld.so 验证我的 libHelloWorld.so 文件是 32 位,并获得 ELF32 位 MSB 动态 lib SPARC 版本 1,动态链接,未剥离,没有可用的调试信息。我还运行了 java -version 并获得了 Java HotSpot(TM) Server VM(构建,混合模式) 所以看起来这个 JVM 正在 32 位运行模式。

为什么我仍然收到此错误?

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 MSB 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-09-02 19:58:20

是的 :-)。所以不允许我提交一个单词的答案,所以这里有一些可能有用的 Google 点击

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文