使我的 Java 程序易于分发
我已经通过 exe 安装程序在 PC 上安装了 Java 3D API,该安装程序只是使用 j3dcore.jar
、vecmath.jar
、j3dutils.jar< 创建了一个新目录/code> 在 lib 子目录中,
j3dcore-ogl.dll
在 bin 子目录中。
Netbeans 没有任何问题,我的代码编译和执行顺利,但是一旦我构建了我的项目并尝试从命令提示符运行它,我收到一个 UnsatisfiedLinkError
说 no j3dcore-ogl in java。库.path
。
Google 来救援并给了我 3 个可行的解决方案:
- 将 dll 文件复制到我的 JRE 的 bin 目录中
- 通过将 dll 文件的路径添加到库路径(
java -Djava.library.path=dllpath)
- 使用
System.load()
加载程序中的 dll (实际上,我无法让这个工作)
我的问题是:是否有一个优雅的解决方案来解决这个问题,我错过了?
对于每个想要在其上使用此程序的不同 PC,他必须复制 dll 或将其添加到库路径才能运行,这似乎很乏味。 (附带问题:为什么 Netbeans 的 dll 没有问题?)
I have installed the Java 3D API on PC via the exe installer, which simply created a new directory with j3dcore.jar
, vecmath.jar
, j3dutils.jar
in a lib sub-directory and j3dcore-ogl.dll
in a bin sub-directory.
Netbeans had no issues and my code compiled and executed smoothly, however once I built my project and tried to run it from the command prompt I got an UnsatisfiedLinkError
saying that no j3dcore-ogl in java.library.path
.
Google came to the rescue and gave me 3 viable solutions:
- by copying the dll file into my JRE's bin directory
- by adding the path of the dll file to the library path (
java -Djava.library.path=dllpath
) - load the dll in the program with
System.load()
(I couldn't get this one to work, actually)
My question is: Is there an elegant solution to this problem, that I missed?
It seems tedious that for each different PC someone would like to use this program on, he'd have to either copy the dll or add it to the library path before it can run. (Side question: How come Netbeans didn't have a problem with the dll?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的意思是“易于最终用户使用”,请查看 Java Web Start。
有路人问:
是的,但是好多了。您可以将每个平台的本机打包在单独的 Jars 中,并仅将它们提供给使用该本机的平台,即使在 32 和 32 之间分区下载。本机的 64 位版本。
JWS 将本机放在应用程序的运行时类路径上,准备加载到代码中。
对于最终用户来说,这一切都是自动发生的,他们单击链接,在询问时批准信任对话框,然后应用程序就会安装 - 可能与桌面集成一起,并像魔术一样出现在屏幕上。
JWS 应用程序。使用本机的需要以
所有权限
安全级别进行分发,因为 JVM 无法保证任何“本机”的操作。If you mean 'easy for the end user' look to Java Web Start.
A passer-by asks:
Yes, but much, much better. You can package the natives for each platform in separate Jars, and supply them only to the platform that uses that native, even so far as partitioning the download between 32 & 64 bit versions of the natives.
JWS puts the natives on the run-time class-path of the application, ready for loading in code.
This all happens automatically for the end user, they click a link, approve the trust dialog(s) when asked, and the application installs - possibly with desktop integration, and appears on screen like magic.
JWS apps. that use natives need to be distributed as
all-permissions
security level, because the JVM cannot guarantee the actions of anything that 'goes native'.编辑 - 重新阅读您的问题后,您的问题听起来有所不同。不过,我可以像这样运行,只需将所有 dll 文件放在与启动 java 进程的 .bat 文件相同的目录中即可:
java -classpath ./YourJar.jar;./ lib/j3dcore.jar;./lib/vecmath.jar;./lib/j3dutils.jar package.MainClass
这适用于多个用户的电脑,所以我知道只需将其放在工作目录中就可以了.
我相信这取决于所使用的 Java 版本 - 64 位或 32 位。正确的 dll 文件(同名)需要位于工作目录中。
我认为当使用错误的 dll 时我遇到了类似的问题,并且它与操作系统无关(如果您的 64 位操作系统安装了 32 位 Java,则需要 32 位 j3dcore-ogl.dll 文件)。
所以问题是,您使用的是哪个版本的 Java(在 IDE 之外运行时),以及您将哪个版本的 dll(如果有)放入工作目录中?我的路径设置中不需要任何 dll 文件来使其在其他电脑上运行,并且没有使用 System.load(),并且没有将文件复制到我的用户的 JRE/bin 目录中 - 所以我知道这是可能的你提到的3个选项。
Edit - After re-reading your question, your issue sounds different. However I'm able to get my running like so, by just dropping all dll files in the same directory as the .bat file starting the java process:
java -classpath ./YourJar.jar;./lib/j3dcore.jar;./lib/vecmath.jar;./lib/j3dutils.jar package.MainClass
And that works on multiple user's PCs, so I know simply dropping it in the working directory works.
I believe it depends on the version of Java being used - 64 bit or 32 bit. The correct dll file (of the same name) needs to be in the working directory.
I think I was getting a similar problem when the wrong dll was being used, and it's not OS-dependent (if your 64 bit OS has 32-bit Java installed, you'd need the 32 bit j3dcore-ogl.dll file).
So the question is, which version of Java are you using (when running outside of your IDE), and which version of the dll are you putting (if any) in the working directory? I don't need any dll files in my path settings to get this working on other's PCs, and did not use System.load(), and did NOT copy files into my user's JRE/bin directory - so I know this is possible without the 3 options you mention.
如果将 dll 放在与 Jar 相同的目录中,它可以工作吗?
如果是的话,你可以考虑这样分发。
If you put the dlls in the same directory than you Jar, does it work?
If yes, you could consider distributing it like this.
我猜想 DLL 在 Windows 上的 %PATH% 中的所有文件夹中进行搜索。 (UNIX 版本的 LD_LIBRARY_PATH)
您可以尝试将 dll 的路径添加到 %path% 变量中吗?
您似乎正在尝试打包一个具有许多 jars 作为依赖项的产品。您可能会受益于 One-Jar。它声称有本机 dll 支持。
I guess DLL are searched in all folders in %PATH% on windows. (LD_LIBRARY_PATH for UNIX flavors)
Could you try by adding the path to dll to %path% variable?
It appears that you are trying package a product with many jars as dependencies. You may benefit from One-Jar. It claims to have native dll support.