此代码片段应该做什么:检测所有串行端口并将端口名称打印到屏幕上。
问题:在eclipse中运行时,程序检测到串口正常;但是,在导出可运行的 jar 并运行该 jar 后,该 jar 检测到 NO 端口。我非常怀疑这与实际代码本身有什么关系(因为它在 eclipse 中运行得很好),但我已经把代码片段放在里面以防我错过了什么。
我尝试过/已经做过/看过的:
我考虑过它可能只是我的电脑,所以我已经尝试在另一台电脑上运行 jar,但收到了相同的结果。我还确保一切都在正确的位置(jar 文件、.dll 文件和属性文件)。我四次检查以确保我的构建路径包含正确的文件。
我已经探索过: http ://www.oracle.com/technetwork/java/index-139971.html
我花了最后一个小时搜索与我类似的问题的网站和 SO 问题,但我没有找到解决方案。
这是我基于我的代码的网站:http ://en.wikibooks.org/wiki/Serial_Programming/Serial_Java#JavaComm_API
问题: 为什么会发生这种情况/我该如何解决?
附加信息:我正在使用javax.comm
32 位版本的 jdk 1.8.0_31,运行 windows 8。jar 文件运行良好(有一个单独的组件可以完美运行)。编译期间没有错误(或警告),也没有抛出异常。我正在使用java -jar program.jar
. 我知道我的电脑有一个串口,因为当我运行程序时 Eclipse 显示 COM1。(该程序也能够检测并行端口,但仅在 eclipse 中)
boolean portsDetected = false;
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
while (portIdentifiers.hasMoreElements()) {
CommPortIdentifier pid = (CommPortIdentifier)portIdentifiers.nextElement();
if ((pid.getPortType() == CommPortIdentifier.PORT_SERIAL)) {
System.out.println(pid.getName());
portsDetected = true;
}
}
if (!portsDetected) {
System.out.println("No serial ports detected");
return 0;
}
如果您还有其他需要了解或想看的内容,请询问,您将收到!