Windows 上的 RxTx 安装 java.lang.NoClassDefFoundError: gnu/io/CommPort
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12543202/
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
RxTx installation on windows java.lang.NoClassDefFoundError: gnu/io/CommPort
提问by Tobia
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.jar
到jre/lib/ext
文件夹,但是我还是NoClassDefFoundError
没有这个文件夹自动纳入到全局类路径?
Thanks
谢谢
采纳答案by linski
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:
是的,它会自动进入类路径,但 RXTXcomm 使用 JNI /native 外部库(.so 和 .dll 文件),在命令行中运行程序时,您必须提供它们的路径:
java -jar yourprogram.jar -Djava.library.path="PATH_TO_EXTERNAL_LIBRARIES"
for linux:
对于Linux:
suppose you unpacked the rxtx.zipto /home/user/
假设您将rxtx.zip解压到 /home/user/
if you have 32bit x86 platofrm:
如果您有 32 位 x86 平台:
PATH_TO_EXTERNAL_LIBRARIES = /home/user/Linux/i686-unknown-linux-gnu/
PATH_TO_EXTERNAL_LIBRARIES = /home/user/Linux/i686-unknown-linux-gnu/
if you have 64bit x86 platform the it would be:
如果您有 64 位 x86 平台,它将是:
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:
对于窗户:
suppose you downloaded and unpacked it to C:\rxtxt
假设你下载并解压到 C:\rxtxt
PATH_TO_EXTERNAL_LIBRARIES = C:\rxtxt\Windows\i368-mingw32\
PATH_TO_EXTERNAL_LIBRARIES = C:\rxtxt\Windows\i368-mingw32\
If you find it cumbersome to do it from command line you can do it from yout code (beforeopening port via RXTXcomm):
如果您发现从命令行执行此操作很麻烦,您可以从您的代码执行此操作(在通过 RXTXcomm 打开端口之前):
System.setProperty("java.library.path","PATH_TO_EXTERNAL_LIBRARIES");
EDIT:
编辑:
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:
当然,除了上述所有内容之外,您还必须将 RXTXcomm.jar 放在您的类路径中。如果从命令行作为 jar 打包程序 - yourprogram.jar - 在 jar 中运行,您必须有一个 META-INF 文件夹,其中包含 MANIFEST.MF 和以下条目:
Class-Path: lib/RXTXcomm.jar
Main-Class: pkg.Main
and yourprogram.jar must be in folder which has folder lib in which is RXTXcomm.jar, also the class with
并且 yourprogram.jar 必须位于文件夹 lib 所在的文件夹中,其中 RXTXcomm.jar 也是该类
public static void main(String[] args)
method must be called Main and reside in package named pkg (just replace pkg.Main with whatever you have).
方法必须称为 Main 并驻留在名为 pkg 的包中(只需将 pkg.Main 替换为您拥有的任何内容)。
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
然后你可以成功运行你的程序并打开一个串口(如果你有的话)。这种方法还消除了在 jre/lib/ext 文件夹中复制任何内容的需要
EDIT^2:
编辑^2:
or, if you don't want to pack your program in jar, position yourself in folder which contains the folder pkg and write:
或者,如果您不想将程序打包到 jar 中,请将自己置于包含文件夹 pkg 的文件夹中并写入:
java -cp PATH_TO_RXTX/RXTXcomm.jar -Djava.library.path="PATH_TO_EXTERNAL_LIBRARIES" pkg.Main
(all paths can be relative or absolute)
(所有路径都可以是相对的或绝对的)
EDIT^3:
编辑^3:
but I would recommend Java Simple Serial Connectorinstead of RXTXcomm:
但我会推荐Java Simple Serial Connector而不是 RXTXcomm:
- it handles heavy load from multiple threads as opposed to RXTXcomm (tested in production)
- external libraries are packed in jar so there is no need for setting java.library.path
- 它处理来自多个线程的重负载,而不是 RXTXcomm(在生产中测试)
- 外部库打包在 jar 中,因此无需设置 java.library.path