在 JVM 上运行基于 JNI 的 .jar

发布于 2024-12-08 20:55:40 字数 540 浏览 0 评论 0原文

我编写了一个 java 程序,它通过 JNI 使用一些本机方法。我的java代码基本上实现了一个GUI来从我的.dll调用本机方法。当我从 IDE (netbeans) 运行代码时,一切似乎都工作正常,但是当我尝试运行 .jar 文件时,它似乎无法访问 .dll 方法。

出于兼容性原因,我用32位库编译了java程序(即使我运行的是64位版本的windows7)。我尝试在 32 位 java 平台上运行 .jar,但 GUI 甚至没有像通常使用 64 位版本那样显示。我也只是使用“system.loadlibrary”命令,因此 .dll 路径应该是相对的。

我收到的错误是“无法在 AMD 64 位平台上加载 IA 32 位 .dll ...等等”。当我从 32 位版本的 java 运行时,它显示“无法访问 jarfile ...”。有什么办法解决这个问题吗?该程序从我的IDE运行的事实让我认为应该有一种方法可以做到这一点。理想情况下,我希望我的程序也能在 32 位机器上运行,所以我不想让程序成为 64 位。

另外,编译器会抛出警告“引导类路径未与源 1.6 一起设置”,但我不确定这是否与我的问题有关。

I have a java program i wrote which uses some native methods via JNI. my java code basically implements a GUI to call native methods from my .dll. everything seems to work fine when i run the code from my IDE (netbeans), but when i try to run the .jar file it doesn't seem to be able to access the .dll methods.

i've compiled the java program with the 32-bit library for compatibility reasons (even though i am running a 64-bit version of windows7). i tried running the .jar with the 32-bit java platform but the GUI didn't even display like it normally would using the 64-bit version. i'm also just using the "system.loadlibrary" command so the .dll path should be relative.

the error i get is "can't load IA 32-bit .dll on a AMD 64-bit platform ... blah blah". when i run from the 32-bit version of java it says "unable to access jarfile ...". is there any way around this? the fact that the program runs from my ide makes me think there should be a way to to this. ideally i'd want my program to run on 32-bit machines as well so i don't want to make the program 64-bit.

also, the compiler throws the warning "bootstrap classpath not set in conjunction with source 1.6", but i'm not sure if this has anything to do with my problem.

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

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

发布评论

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

评论(2

深海不蓝 2024-12-15 20:55:40

系统属性java.library.path定义了Java在使用System.loadLibrary()加载库时查看的路径。在 Windows 上,默认情况下,它将包含当前目录(即应用程序启动的目录)以及 JRE bin 目录和 Windows 的 system32 目录。

您可以尝试在启动 JVM 时输入参数 -Djava.library.path= 并查看是否有影响。

您还可以使用 System.load() 使用绝对路径强制加载库文件。

The system property java.library.path defines the paths that Java looks at when loading libraries with System.loadLibrary(). On Windows, by default this will have the current directory (i.e. the directory the app was started from) as well as the JRE bin directory and Windows' system32 directory.

You could try putting the arguments -Djava.library.path=<path to your DLL file> when you start up the JVM and see if that makes a difference.

You can also force load a library file using an absolute path using System.load() instead.

冰葑 2024-12-15 20:55:40

我尝试将.dll复制到windows/system32目录

下。

java/jdk1.7.0/bin目录

应该可以工作,但是位置错误。

并与 .jar 位于同一目录

JNI DLL 的最佳位置是执行 JAR 文件时的当前目录。

但这些都没有解决问题。

那是哪一个问题呢?您尚未提供错误消息。

I tried copying the .dll to the windows/system32 directory

No.

the java/jdk1.7.0/bin directory

Should have worked, but the wrong place.

and to the same directory as the .jar

No.

The best place for JNI DLLs is in the current directory when you execute the JAR file.

but none of these fixed the problem.

Which problem is that? You haven't provided the error message yet.

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