Windows上的RxTx安装java.lang.NoClassDefFoundError:gnu / io / CommPort [英] RxTx installation on windows java.lang.NoClassDefFoundError: gnu/io/CommPort
问题描述
我把 rxtxcomm.jar
放到 jre / lib / ext
文件夹中,但我仍然得到 NoClassDefFoundError
这个文件夹不会自动进入全局类路径吗?
I put rxtxcomm.jar
into jre/lib/ext
folder, but I still get NoClassDefFoundError
Isn't this folder automatically taken into the global classpath?
谢谢
推荐答案
是的,它会被自动带到类路径,但RXTXcomm使用JNI /本机外部库(.so和.dll文件),在运行程序时必须提供它们的路径命令行:
yes it is taken automatically to classpath, but RXTXcomm uses JNI /native external libraries (.so and .dll files), you must provide the path to them when running your program in command line:
java -jar yourprogram.jar -Djava.library.path="PATH_TO_EXTERNAL_LIBRARIES"
for linux:
for linux:
假设您解压缩 rxtx.zip 到
/ home / user /
suppose you unpacked the rxtx.zip to /home/user/
如果你有32位x86 platofrm:
if you have 32bit x86 platofrm:
PATH_TO_EXTERNAL_LIBRARIES = / home / user / Linux / i686-unknown-linux-gnu /
PATH_TO_EXTERNAL_LIBRARIES = /home/user/Linux/i686-unknown-linux-gnu/
如果你有64位x86平台那么它就行了d be:
if you have 64bit x86 platform the it would be:
PATH_TO_EXTERNAL_LIBRARIES = / home / user / Linux / x86_64-unknown-linux-gnu /
PATH_TO_EXTERNAL_LIBRARIES = /home/user/Linux/x86_64-unknown-linux-gnu/
for windows:
for windows:
假设你下载并解压缩到C:\ rxtxt
suppose you downloaded and unpacked it to C:\rxtxt
PATH_TO_EXTERNAL_LIBRARIES = C:\ rxtxt \ Windows\i368-mingw32 \
PATH_TO_EXTERNAL_LIBRARIES = C:\rxtxt\Windows\i368-mingw32\
如果你发现从命令行执行它很麻烦,你可以从你的代码(之前通过RXTXcomm打开端口)这样做:
If you find it cumbersome to do it from command line you can do it from yout code (before opening port via RXTXcomm):
System.setProperty("java.library.path","PATH_TO_EXTERNAL_LIBRARIES");
编辑:
当然,你除了以上所有内容之外,还必须将RXTXcomm.jar放在类路径中。如果从命令行运行jar包装程序 - yourprogram.jar - 在jar中你必须有一个包含MANIFEST.MF的META-INF文件夹,其中包含以下条目:
of course, you must put RXTXcomm.jar in your classpath in addition to all of the above. If running from command line as jar packaged program - yourprogram.jar - inside the jar you must have a META-INF folder that contains MANIFEST.MF with the following entries:
Class-Path: lib/RXTXcomm.jar
Main-Class: pkg.Main
和yourprogram.jar必须位于文件夹中,其中包含文件夹lib,其中包含RXTXcomm.jar,同时
该类包含
and yourprogram.jar must be in folder which has folder lib in which is RXTXcomm.jar, also the class with
public static void main(String[] args)
方法必须称为Main并驻留在名为pkg的包中(只需将pkg.Main替换为您拥有的任何内容)。
method must be called Main and reside in package named pkg (just replace pkg.Main with whatever you have).
然后,您可以成功运行程序并打开串行端口有一个。这种方法也消除了在jre / lib / ext文件夹中复制任何内容的需要
Then you can run your program succesfully and open a serial port if you have one. This approach also eliminates the need to copy anything in jre/lib/ext folder
编辑^ 2:
或者,如果您不想将程序包装在jar中,请将自己放在包含文件夹pkg的文件夹中并写入:
or, if you don't want to pack your program in jar, position yourself in folder which contains the folder pkg and write:
java -cp PATH_TO_RXTX/RXTXcomm.jar -Djava.library.path="PATH_TO_EXTERNAL_LIBRARIES" pkg.Main
(所有路径可以是相对的或绝对的)
(all paths can be relative or absolute)
编辑^ 3:
但我建议 Java简单串行连接器而不是RXTXcomm:
but I would recommend Java Simple Serial Connector instead of RXTXcomm:
- 它处理来自多个线程的重负载而不是RXTXcomm(在生产中测试)
- 外部库在jar中打包所以有无需设置java.library.path
这篇关于Windows上的RxTx安装java.lang.NoClassDefFoundError:gnu / io / CommPort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!