在 Java 小程序中动态加载本机库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/643264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:11:01  来源:igfitidea点击:

Dynamically loading a native library inside Java applet

javaappletjava-native-interfacerxtx

提问by Ariod

I am trying to write a Java applet that will read from a user's serial port. For this I am using the Java Comm APIin conjuction with the RXTX library. I've successfully managed to read data through a local Java application but I can't seem to do the same from within an applet. The problem is that the RXTX library is a native library and I haven't found a way to load this library inside the Java applet. I've found two interesting articles (Using JNI in Appletswhich references to this article), but both are over 10 years old and I am wondering if there is a newer solution. Or if you can suggest an alternative, I would love to hear it!

我正在尝试编写一个将从用户的串行端口读取的 Java 小程序。为此,我将Java Comm APIRXTX 库结合使用。我已经成功地设法通过本地 Java 应用程序读取数据,但我似乎无法从小程序中读取数据。问题是 RXTX 库是本机库,我还没有找到在 Java 小程序中加载这个库的方法。我发现了两篇有趣的文章(引用本文的Applets中使用JNI),但都已经超过 10 年了,我想知道是否有更新的解决方案。或者,如果您可以提出替代方案,我很想听听!

回答by

Use Java Web Start. You can include the native library as a signed jar and everything should work.

使用 Java Web Start。您可以将本机库作为签名的 jar 包含在内,一切都应该可以正常工作。

1) put the native librarys in a jar in the ROOT directory 2) sign ALL the jars 3) write a JNLP referencing all the jars (go do a search on how to write jnlp's)

1) 将本机库放在 ROOT 目录中的一个 jar 中 2) 对所有 jar 进行签名 3) 编写一个引用所有 jar 的 JNLP(去搜索一下如何编写 jnlp)

after that it should work

之后它应该工作

回答by Tom Hawtin - tackline

Firstly I'd suggest doing that in an Applet is probably a bad idea.

首先,我建议在 Applet 中这样做可能是个坏主意。

If you do go ahead anyway, JNLPAppletLaunchermay help.

如果您仍然继续,JNLPAppletLauncher可能会有所帮助。

回答by Brian Agnew

I don't know about applets, other than they're locked down heavily wrt. security/accessing the machine's resources etc.

我不知道小程序,除了它们被严重锁定之外。安全/访问机器的资源等。

If you need a web-deployable solution, have you looked at Java Web Start? See in particular this FAQ entry

如果您需要一个 Web 部署解决方案,您是否看过Java Web Start?请特别参阅此常见问题解答条目

回答by Charlie Martin

Frankly, trying to access devices from a Java applet is a bad idea even if you could make it work, and loading a dynamic library into the conventional applet is an even worse idea; in fact, I'd be astounded if you could find a way to trick a modern browser into doing it. (Consider the possible security implications.)

坦率地说,尝试从 Java 小程序访问设备是一个坏主意,即使您可以使它工作,而将动态库加载到传统小程序中也是一个更糟糕的主意;事实上,如果你能找到一种方法来欺骗现代浏览器来做这件事,我会感到震惊。(考虑可能的安全隐患。)

You're better off, as suggested on other questions, building an app that can be launched with Web Start.

根据其他问题的建议,您最好构建一个可以通过 Web Start 启动的应用程序。

回答by johnstosh

I'd be real interested in the answer here also. My suggestion would be to install the .jar and .dll for RXTX into the \program files\java\ tree. You could install them on the target system before trying to use the applet. I like the idea of using Java Web Start. If you sign the applet, can't you read/write any file on the machine? So you could install the .jar and .dll and then use them afterward?

我也对这里的答案很感兴趣。我的建议是将 RXTX 的 .jar 和 .dll 安装到 \program files\java\ 树中。您可以在尝试使用小程序之前将它们安装在目标系统上。我喜欢使用 Java Web Start 的想法。如果你签署了小程序,你不能在机器上读/写任何文件吗?所以你可以安装 .jar 和 .dll 然后使用它们?

For windows, I suggest using "Windows Java Serial Com Port Driver" rather than rxtx. You can download it from http://www.engidea.com/blog/informatica/winjcom/winjcom.htmlI've found it much easier to install and use and much more bug-free. I was unable to use RXTX with USB serial devices because RXTX would crash when the USB port was unplugged. winjcom solved these problems for me, and more.

对于 Windows,我建议使用“Windows Java Serial Com Port Driver”而不是 rxtx。您可以从http://www.engidea.com/blog/informatica/winjcom/winjcom.html下载它我发现它的安装和使用要容易得多,而且没有更多的错误。我无法将 RXTX 与 USB 串行设备一起使用,因为拔下 USB 端口时 RXTX 会崩溃。winjcom 为我解决了这些问题,等等。

I'm still trying to decide how to deploy the DLL, but I'm not sweating it because I don't expect the DLL for "Windows Java Serial Com Port Driver" to change. -Stosh

我仍在尝试决定如何部署 DLL,但我并不担心,因为我不希望“Windows Java 串行通讯端口驱动程序”的 DLL 发生变化。-斯托什