【发布时间】:2017-02-28 03:31:23
【问题描述】:
在我的应用程序中,我想与 Arduino 板进行一些通信。为了实现串口通信,我想结合使用Netty框架和RXTX传输库。
- http://netty.io
- https://mvnrepository.com/artifact/io.netty/netty-all/4.1.6.Final
- https://mvnrepository.com/artifact/io.netty/netty-transport-rxtx/4.1.6.Final
所以我在我的 Gradle 构建配置中添加了以下几行:
dependencies {
compile group: 'io.netty', name: 'netty-all', version: '4.1.5.Final'
compile group: 'io.netty', name: 'netty-transport-rxtx', version: '4.1.5.Final'
...
}
现在编译时依赖解决了,我可以构建项目而没有任何错误。
我的构建配置使用以下命令生成一个可执行的 JAR:
jar {
manifest {
attributes 'Main-Class': 'de.project.Main',
'Implementation-Title': 'My Project',
'Implementation-Version': version
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
但由于 RXTX 库使用本机库,所以在执行代码时出现以下异常:
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
at de.project.communication.SerialConnector.getSerialPorts(SerialConnector.java:83)
at de.project.Main.main(Main.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
异常告诉我,我需要 RXTX 的平台相关本机库,例如:
librxtxSerial.jnilib(用于 OSX)
所以我的问题是:将本机库添加到我的 Gradle 构建的最佳做法是什么?在 IDE 之外运行我的项目时,我如何告诉 IntelliJ 也使用这些本机库?到目前为止,我还没有在互联网上找到任何令人满意的答案。
【问题讨论】:
标签: java gradle netty native rxtx