搜索
首页Javajava教程Java 类未找到异常

顾名思义,当 Java 虚拟机 (JVM) 尝试加载特定类时,Java 中会发生 ClassNotFoundException。在你指定的类的路径中找不到请求的类,这意味着你指定的类的路径被破坏了,这个问题在Java世界里确实很常见。因此,ClassNotFoundException在Java中也很常见。这个问题对于 Java 初学者来说非常困惑,ClassNotFoundException 必须被 catch 或抛出给调用者 ClassNotFoundException 是一个受检查的异常。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Java中ClassNotFoundException的语法如下:

java.lang.ClassNotFoundException:Class_name at location

Java 中 ClassNotFoundException 的工作原理

  • 当应用程序尝试加载类时,类加载器无法在指定类的路径中找到该类,即 Java 中出现 ClassNotFoundException。
  • 使用Class.forName和ClassLoader.loadClass加载类,同时传递类的字符串名称作为参数,但在指定类的路径中找不到,这是java.lang的常见原因之一Java 中的 .ClassNotFoundException。
  • Java 中的 ClassNotFoundException 必须被捕获或抛出给调用者,因为它是一个受检查的异常。
  • 该类是使用类加载器间接加载的。结果,Java中的ClassNotFoundException在运行时出现。 Java 编译器无法在运行时知道某个类是否存在于指定类的路径中。
  • 尝试使用 Class.forName 加载 JDBC 驱动程序并且不在类的路径中添加 jar 文件是 Java 中 ClassNotFoundException 的常见示例之一。
  • 考虑下面的程序来演示 Java 中的 ClassNotFoundException:
//a class called program is defined
public class Program
{
//main method is called
public static void main(String args[])
{
//class not found exception is defined using try and catch block
try
{
// the forname method in class class looks for the mentioned class
Class.forName("The Class do not Exist");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}

上述程序的输出如下图所示:

Java 类未找到异常

在上面的程序中,定义了一个名为Program的类。然后调用main方法。然后使用 try 和 catch 块定义类未找到异常。类加载器试图查找的类不存在,并且类中的 forname 方法查找所提到的类,但未能找到;因此抛出 ClassNotFoundException。程序的输出如上面的快照所示。

构造函数

Java中有几个ClassNotFoundException的构造函数。他们是:

  • ClassNotFoundException(): 构造一个新的 ClassNotFoundException,其中包括堆栈的当前跟踪。
  • ClassNotFoundException(String): 构造一个新的 ClassNotFoundException,其中包括指定的堆栈的当前跟踪和详细消息。
  • ClassNotFoundException(IntPtr, JniHandleOwnership): 在创建 JNI 对象期间,在管理其表示时,会使用此构造函数,并且运行时会调用它。
  • ClassNotFoundException(String, Throwable): 构造一个新的 ClassNotFoundException,其中包括堆栈的当前跟踪以及指定的详细消息以及尝试加载类时发生的异常.

Java ClassNotFoundException 示例

以下是下面提到的示例:

示例#1

演示 ClassNotFoundException 的 Java 程序:

 代码:

//a class called exceptiondemo is defined
public class Exceptiondemo
{
//a string variable is defined
private static final String DRIVE_CLASS = "com.mysql.jdbc.Driver";
//main method is called including the exception
public static void main(String[]  args) throws Exception
{
System.out.println("MySQL JDBC driver loading attempt");
//the forname method in class class looks for the mentioned class
Class.forName(DRIVE_CLASS);
}
}

上述程序的输出如下图所示:

Java 类未找到异常

在上面的程序中,定义了一个名为Exception demo的类。然后调用main方法。然后定义一个字符串变量,向其分配 JDBC 驱动程序路径。然后调用 main 方法,该方法抛出异常。类加载器尝试查找指定类的 JDBC 驱动程序路径,并且类中的 forname 方法查找所提到的类,但未能找到;因此抛出 ClassNotFoundException。程序的输出如上面的快照所示。

示例#2

演示 ClassNotFoundException(String) 的 Java 程序

代码:

//a class called check is defined
public class Check
{
//main method is called
public static void main(String args[])
{
//class not found exception is defined using try catch block
try
{
//the forname method in class class looks for the mentioned class
Class.forName("Demonstrating class not found exception");
}
catch(ClassNotFoundException e)
{
//the string specified along with the class not found exception is displayed.
System.out.println("There is no class as specified in the path " + e);
}
}
}

上述程序的输出如下图所示:

Java 类未找到异常

在上面的程序中,定义了一个名为check的类。然后调用main方法。然后调用main方法。然后使用 try 和 catch 块定义类未找到异常。  然后 class 中的 forename 方法查找所提到的类,但没有找到;因此,抛出 ClassNotFoundException 并显示指定为详细消息的字符串以及未找到类异常。程序的输出如上面的快照所示。

如何避免 ClassNotFoundException?

避免 ClassNotFoundException 的步骤:

  • 必须找出导致该 jar 文件存在问题的类的文件。
  • 我们应该检查类的路径或类路径是否包含该 jar。如果该 jar 不存在于类路径或类路径中,则必须将该类添加到类或类路径路径中。
  • 如果类存在于类或类路径路径中,则很可能存在类路径或类路径的覆盖,或者 jar 文件中指定的路径或用于启动的脚本正在被使用该应用程序。为了解决这个问题,我们需要找到应用程序正在使用的类的确切路径。

结论

在本教程中,我们通过定义了解 Java 中的 Class Not Found Exception 的概念、Java 中的 Class Not Found Exception 的语法、Java 中的 Class Not Found Exception 的工作原理以及通过示例及其输出来了解它们的构造函数。

以上是Java 类未找到异常的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器