[英]Prevent NoClassDefFoundError from crashing program
I'm writing a Java program for my work-study program which relies on the RXTX serial drivers. 我正在为我的工作学习程序编写一个Java程序,该程序依赖于RXTX串行驱动程序。 It is running well on my testing machines, however I have noticed that when run on a machine that does not have RXTX installed the application fails to open.
它在我的测试机器上运行良好,但是我注意到当在没有安装RXTX的机器上运行时,应用程序无法打开。 In the console it has thrown the "java.lang.NoClassDefFoundError" exception for "gnu/io/CommPortIdentifier".
在控制台中,它为“gnu / io / CommPortIdentifier”抛出了“java.lang.NoClassDefFoundError”异常。 I put this into a try/catch so that it would instead display a message to the user telling them to check their RXTX driver installation rather than simply exiting the program.
我将它放入try / catch中,以便它向用户显示一条消息,告诉他们检查他们的RXTX驱动程序安装,而不是简单地退出程序。 However it doesn't actually do this, still just closes out as soon as it hits that line.
然而它实际上并没有这样做,只要它碰到那条线就会关闭。 Any ideas?
有任何想法吗? Thanks!
谢谢!
EDIT: Some code for ya: 编辑:雅的一些代码:
Enumeration sportsAll = null;
Vector<String> v = new Vector();
SerialPort sp;
CommPortIdentifier portID;
String currString;
try {
sportsAll= CommPortIdentifier.getPortIdentifiers();
} catch (Exception e) {
v.addElement("Check RXTX Drivers");
}
The "sportsAll= CommPortIdentifier" line is the one that throws the error “sportsAll = CommPortIdentifier”行是抛出错误的行
It's because you try to catch Exception
which is not a parent class for NoClassDefFoundError
see JavaDoc . 这是因为您尝试捕获不是
NoClassDefFoundError
的父类的Exception
,请参阅JavaDoc 。 Catch the concrete exception class instead. 请改为捕获具体的异常类。
Better approach is to check for availability of the driver itself. 更好的方法是检查驱动程序本身的可用性。 For example:
例如:
private static boolean isDriverAvailable() {
boolean driverAvailable = true;
try {
// Load any class that should be present if driver's available
Class.forName("gnu.io.CommPortIdentifier");
} catch (ClassNotFoundException e) {
// Driver is not available
driverAvailable = false;
}
return driverAvailable;
}
java.lang.Error
states java.lang.Error
状态
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch .
Error是Throwable的子类,表示合理的应用程序不应该尝试捕获的严重问题。 Most such errors are abnormal conditions.
大多数此类错误都是异常情况。
And java.lang.NoClassDefFoundError
extends LinkageError
(which extends Error
). 并且
java.lang.NoClassDefFoundError
扩展了LinkageError
(扩展了Error
)。 You shouldn't catch any Error
at all in a try-catch block. 你不应该在try-catch块中捕获任何
Error
。
You will have to write a code to check if RXTX is installed first before executing your rest of the code. 在执行其余代码之前,您必须编写代码以检查是否先安装了RXTX。
This is because NoClassDefFoundError
extends Error
( not Exception
). 这是因为
NoClassDefFoundError
扩展了Error
( 不是 Exception
)。
The best way would be to catch NoClassDefFoundError
itself. 最好的方法是捕获
NoClassDefFoundError
本身。 You can also catch either Error
or Throwable
. 您还可以捕获
Error
或Throwable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.